Python进阶教程:自动化办公实战
🕒 阅读时间:6 分钟📝 字数:1032👀 阅读量:Loading...
本文摘要
这篇Python办公自动化教程介绍了几个常用库的实战应用,包括:openpyxl处理Excel(读取/写入/合并)、python-docx操作Word文档、pdfplumber提取PDF文本…
目录
本文是 Python 入门教程系列 的第 16 篇(扩展篇)。Python 最接地气的应用场景之一就是自动化办公,本篇用实战演示如何用 Python 处理 Excel、Word、PDF 和批量文件。
一、办公自动化常用库
| 库 | 用途 |
|---|---|
| openpyxl | 读写 Excel(.xlsx) |
| pandas | 数据分析 + Excel 处理 |
| python-docx | 操作 Word 文档 |
| PyPDF2 / pdfplumber | 处理 PDF |
| Pillow | 图像处理 |
| pathlib / shutil | 文件与目录操作 |
安装:pip install openpyxl python-docx pdfplumber pillow
二、Excel 处理(openpyxl)
2.1 读取 Excel
import openpyxl
# 打开工作簿wb = openpyxl.load_workbook("sales.xlsx")ws = wb.active
# 读取单元格print(ws["A1"].value)print(ws.cell(row=2, column=1).value)
# 遍历所有行for row in ws.iter_rows(min_row=1, values_only=True): print(row)
# 获取行列数print(ws.max_row, ws.max_column)2.2 写入 Excel
import openpyxl
wb = openpyxl.Workbook()ws = wb.activews.title = "成绩表"
# 写入表头headers = ["姓名", "语文", "数学", "英语"]ws.append(headers)
# 写入数据students = [ ("张三", 90, 85, 92), ("李四", 78, 95, 88), ("王五", 85, 90, 86),]for stu in students: ws.append(stu)
from openpyxl.styles import Fontws["A1"].font = Font(bold=True)ws.column_dimensions["A"].width = 12
wb.save("output.xlsx")print("保存成功")三、批量处理:合并多个 Excel
import openpyxlimport glob
def merge_excels(pattern, output): """合并多个 Excel 文件""" wb_out = openpyxl.Workbook() ws_out = wb_out.active
for file in glob.glob(pattern): wb = openpyxl.load_workbook(file) ws = wb.active print(f"处理:{file}") for row in ws.iter_rows(values_only=True): ws_out.append(row)
wb_out.save(output) print(f"已合并到 {output}")
# merge_excels("data/*.xlsx", "merged.xlsx")四、Word 文档处理
from docx import Document
# 创建文档doc = Document()doc.add_heading("工作报告", level=1)doc.add_paragraph("这是第一段内容。")doc.add_paragraph("这是第二段内容。")
# 添加表格table = doc.add_table(rows=3, cols=2)table.cell(0, 0).text = "项目"table.cell(0, 1).text = "进度"table.cell(1, 0).text = "开发"table.cell(1, 1).text = "80%"
doc.save("report.docx")
# 读取文档doc2 = Document("report.docx")for para in doc2.paragraphs: print(para.text)五、PDF 文本提取
import pdfplumber
with pdfplumber.open("document.pdf") as pdf: print(f"共 {len(pdf.pages)} 页") text = pdf.pages[0].extract_text() print(text[:200])
table = pdf.pages[0].extract_table() if table: for row in table: print(row)六、批量文件处理
from pathlib import Pathimport shutil
src = Path("downloads")dst = Path("organized")
for file in src.iterdir(): if file.is_file(): ext = file.suffix.lstrip(".") or "other" target_dir = dst / ext target_dir.mkdir(parents=True, exist_ok=True) shutil.move(str(file), str(target_dir / file.name)) print(f"移动 {file.name} -> {ext}/" )七、实战:自动生成成绩单报告
import openpyxlfrom docx import Document
def generate_report(excel_file, output_docx): """从 Excel 读取成绩,生成 Word 报告""" wb = openpyxl.load_workbook(excel_file) ws = wb.active
doc = Document() doc.add_heading("学生成绩报告", level=1)
total = 0 count = 0 for row in ws.iter_rows(min_row=2, values_only=True): name, chinese, math, english = row avg = (chinese + math + english) / 3 total += avg count += 1 doc.add_paragraph(f"{name}:平均分 {avg:.1f}")
if count: doc.add_paragraph(f"全班平均分:{total/count:.1f}")
doc.save(output_docx) print(f"报告已生成:{output_docx}")
# generate_report("scores.xlsx", "report.docx")总结
本篇演示了用 openpyxl 处理 Excel、python-docx 处理 Word、pdfplumber 提取 PDF、以及批量文件整理,并实现了一个 Excel 到 Word 的自动化报告生成器。办公自动化的核心是用代码替代重复劳动,建议从自己日常最繁琐的任务开始实践。
Python进阶教程:自动化办公实战
作者:Wang Shijie
本文链接:https://wsj-blog.pages.dev/posts/python-jin-jie-jiao-cheng-zi-dong-hua-ban-gong-shi-zhan/
本文采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。
留言评论
评论资源加载失败,请重试。