在日常办公中,我们常常会被繁琐、重复的任务占据大量时间。Python 作为一门强大的编程语言,拥有丰富的库和工具,能够轻松实现办公自动化,大大提高工作效率。今天,就来给大家分享 10 个实用的 Python 自动化办公案例及源码。
1. 批量处理 Excel 文件
在处理数据时,经常需要对多个 Excel 文件进行相同操作。利用pandas
库可以轻松实现。
1 2 3 4 5 6 7 8 9 10 11 | import pandas as pd import os # 文件夹路径 folder_path = 'your_folder_path' for filename in os.listdir(folder_path): if filename.endswith( '.xlsx' ): file_path = os.path.join(folder_path, filename) df = pd.read_excel(file_path) # 这里可以对df进行各种操作,比如新增一列 df[ 'new_column' ] = df[ '原有列' ] * 2 df.to_excel(file_path, index = False ) |
2. 自动发送邮件
使用smtplib
和email
库,自动发送邮件,适用于定期汇报等场景。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | import smtplib from email.mime.text import MIMEText from email.header import Header # 发件人邮箱 sender = "your_email@example.com" # 收件人邮箱 receivers = [ "recipient_email@example.com" ] # 邮件内容 message = MIMEText( '邮件内容' , 'plain' , 'utf-8' ) message[ 'From' ] = Header( "发件人姓名" , 'utf-8' ) message[ 'To' ] = Header( "收件人姓名" , 'utf-8' ) message[ 'Subject' ] = Header( "邮件主题" , 'utf-8' ) try : smtpObj = smtplib.SMTP( 'smtp.example.com' , 587 ) smtpObj.starttls() smtpObj.login(sender, "password" ) smtpObj.sendmail(sender, receivers, message.as_string()) print ( "邮件发送成功" ) except smtplib.SMTPException as e: print ( "Error: 无法发送邮件" , e) |
3. 批量重命名文件
利用os
库对指定文件夹下的文件进行批量重命名。
1 2 3 4 5 6 7 8 | import os folder_path = 'your_folder_path' count = 1 for filename in os.listdir(folder_path): if os.path.isfile(os.path.join(folder_path, filename)): new_name = f 'new_name_{count}{os.path.splitext(filename)[1]}' os.rename(os.path.join(folder_path, filename), os.path.join(folder_path, new_name)) count + = 1 |
4. 数据清洗
使用pandas
库对数据进行清洗,去除重复值、处理缺失值等。
1 2 3 4 5 6 7 | import pandas as pd df = pd.read_csv( 'your_data.csv' ) # 去除重复行 df = df.drop_duplicates() # 处理缺失值,这里用0填充 df = df.fillna( 0 ) df.to_csv( 'cleaned_data.csv' , index = False ) |
5. 生成 PPT
借助python-pptx
库可以根据数据自动生成 PPT。
1 2 3 4 5 6 7 8 9 10 11 | from pptx import Presentation from pptx.util import Inches prs = Presentation() title_slide_layout = prs.slide_layouts[ 0 ] slide = prs.slides.add_slide(title_slide_layout) title = slide.shapes.title subtitle = slide.placeholders[ 1 ] title.text = "PPT标题" subtitle.text = "PPT副标题" # 后续可以添加更多内容,如图片、表格等 prs.save( 'test.pptx' ) |
6. 自动化测试
使用Selenium
库进行网页自动化测试。
1 2 3 4 5 6 7 8 | from selenium import webdriver driver = webdriver.Chrome() # 查找元素并操作 element = driver.find_element_by_id( 'element_id' ) element.click() # 关闭浏览器 driver.quit() |
7. 提取 PDF 文本
利用PyPDF2
库提取 PDF 文件中的文本。
1 2 3 4 5 6 7 8 9 | import PyPDF2 pdf_file = open ( 'your_pdf.pdf' , 'rb' ) pdf_reader = PyPDF2.PdfReader(pdf_file) text = "" for page_num in range ( len (pdf_reader.pages)): page = pdf_reader.pages[page_num] text + = page.extract_text() print (text) pdf_file.close() |
8. 自动生成报表
结合pandas
和matplotlib
库,生成数据报表并可视化。
1 2 3 4 5 6 7 8 9 10 | import pandas as pd import matplotlib.pyplot as plt df = pd.read_csv( 'data.csv' ) # 假设统计某列数据 data = df[ 'column_name' ].value_counts() data.plot(kind = 'bar' ) plt.title( '数据统计报表' ) plt.xlabel( '类别' ) plt.ylabel( '数量' ) plt.savefig( 'report.png' ) |
9. 自动化文件备份
使用shutil
库实现文件的自动备份。
1 2 3 4 5 6 7 8 9 10 | import shutil import os source_folder = 'your_source_folder' backup_folder = 'your_backup_folder' if not os.path.exists(backup_folder): os.makedirs(backup_folder) for filename in os.listdir(source_folder): file_path = os.path.join(source_folder, filename) if os.path.isfile(file_path): shutil.copy2(file_path, backup_folder) |
10. 任务调度
使用APScheduler
库实现任务的定时执行,比如定时运行数据处理脚本。
1 2 3 4 5 6 | from apscheduler.schedulers.blocking import BlockingScheduler import your_script scheduler = BlockingScheduler() # 每天凌晨1点执行任务 scheduler.add_job(your_script.run, 'cron' , hour = 1 ) scheduler.start() |
通过这些 Python 自动化办公案例,我们可以看到 Python 在提高办公效率方面的巨大潜力。
到此这篇关于10个Python自动化办公的脚本分享的文章就介绍到这了,更多相关Python自动化办公内容请搜索IT俱乐部以前的文章或继续浏览下面的相关文章希望大家以后多多支持IT俱乐部!