摘要:import smtplibfrom email.mime.text import MIMETextdef send_email(subject, body, to_email): smtp_server = "smtp.gmail.com" smtp_por
Python 是自动执行日常任务的绝佳工具,这要归功于它的简单性和广泛的库。以下是有助于在各个领域自动执行常见任务的前 25 个 Python 脚本。
import smtplibfrom email.mime.text import MIMETextdef send_email(subject, body, to_email): smtp_server = "smtp.gmail.com" smtp_port = 587 sender_email = "your_email@gmail.com" sender_password = "your_password" msg = MIMEText(body) msg['Subject'] = subject msg['From'] = sender_email msg['To'] = to_email with smtplib.SMTP(smtp_server, smtp_port) as server: server.starttls server.login(sender_email, sender_password) server.sendmail(sender_email, to_email, msg.as_string)import requestsfrom bs4 import BeautifulSoupdef scrape_weather: url = "https://weather.com" response = requests.get(url) soup = BeautifulSoup(response.text, 'html.parser') print(soup.title.string)import requestsdef download_file(url, save_path): response = requests.get(url) with open(save_path, 'wb') as file: file.write(response.content)import osimport shutildef sort_files(directory): for file in os.listdir(directory): ext = file.split('.')[-1] folder = os.path.join(directory, ext) os.makedirs(folder, exist_ok=True) shutil.move(os.path.join(directory, file), os.path.join(folder, file))批量重命名目录中的文件。库: osimport osdef rename_files(directory, prefix): for i, file in enumerate(os.listdir(directory)): os.rename(os.path.join(directory, file), os.path.join(directory, f"{prefix}_{i}.txt"))将重要文件备份到 zip 文件。库: shutilimport shutildef create_backup(source_dir, backup_file): shutil.make_archive(backup_file, 'zip', source_dir)import tweepydef post_tweet(api_key, api_secret, access_token, access_secret, tweet): auth = tweepy.OAuthHandler(api_key, api_secret) auth.set_access_token(access_token, access_secret) api = tweepy.API(auth) api.update_status(tweet)读/写 Excel 文件。库: openpyxlimport openpyxldef read_excel(file): wb = openpyxl.load_workbook(file) sheet = wb.active for row in sheet.iter_rows: print([cell.value for cell in row])from googletrans import Translatordef translate_text(text, dest_lang): translator = Translator return translator.translate(text, dest=dest_lang).textfrom PyPDF2 import PdfReader, Pdfmergerdef merge_pdfs(pdf_list, output): merger = PdfMerger for pdf in pdf_list: merger.append(pdf) merger.write(output)来源:自由坦荡的湖泊AI
免责声明:本站系转载,并不代表本网赞同其观点和对其真实性负责。如涉及作品内容、版权和其它问题,请在30日内与本站联系,我们将在第一时间删除内容!