Python 操作 Excel 实战:自动化处理表格数据(含 openpyxl 教程)
在日常办公、财务报表、数据清洗等场景中,Excel 表格是不可或缺的工具。
本文将介绍如何使用 Python 的 openpyxl 库高效读写 Excel 文件,实现自动化处理表格数据的能力。
一、为什么选择 openpyxl?
openpyxl 是 Python 中最流行的 Excel 操作库之一,支持 .xlsx 格式文件,功能包括:
-
✅ 创建、读取、写入 Excel 表格
-
✅ 设置单元格格式、颜色、字体
-
✅ 插入图表、合并单元格
-
✅ 自动处理多 sheet 文件
安装方式:
bash
复制编辑
pip install openpyxl
二、读取 Excel 文件内容
python
复制编辑
from openpyxl import load_workbook # 加载工作簿 wb = load_workbook("example.xlsx") ws = wb.active # 默认激活第一个 sheet # 遍历内容 for row in ws.iter_rows(min_row=2, values_only=True): print(row)
📌 values_only=True:返回纯数据而非 Cell 对象
三、写入数据到 Excel
python
复制编辑
from openpyxl import Workbook wb = Workbook() ws = wb.active ws.title = "销售数据" # 写入标题 ws.append(["产品", "销量", "单价", "总价"]) # 写入数据 products = [ ("苹果", 100, 3.5), ("香蕉", 80, 2.2), ("梨子", 60, 4.0), ] for name, qty, price in products: ws.append([name, qty, price, qty * price]) # 保存文件 wb.save("sales.xlsx")
四、设置单元格样式(字体、颜色)
python
复制编辑
from openpyxl.styles import Font, PatternFill ws["A1"].font = Font(bold=True, color="FF0000", size=12) ws["A1"].fill = PatternFill("solid", fgColor="FFFF00")
五、合并单元格 & 设置宽度
python
复制编辑
# 合并单元格 ws.merge_cells("A1:D1") # 设置列宽 ws.column_dimensions["A"].width = 20
六、多 Sheet 操作
python
复制编辑
# 新增 Sheet ws2 = wb.create_sheet("年度汇总") ws2.append(["年份", "收入", "支出", "利润"])
遍历 Sheet:
python
复制编辑
for sheet in wb.sheetnames: print(sheet)
七、Excel 自动统计数据(求和公式)
python
复制编辑
# 在第5行第4列写入 Excel SUM 公式 ws.cell(row=5, column=4).value = "=SUM(D2:D4)"
提醒:openpyxl 不会计算公式值,只负责写入,打开 Excel 后会自动计算。
八、自动化处理案例:批量生成员工工资表
python
复制编辑
data = [ {"name": "张三", "base": 5000, "bonus": 1000}, {"name": "李四", "base": 4800, "bonus": 1200}, ] wb = Workbook() ws = wb.active ws.append(["姓名", "基础工资", "奖金", "总工资"]) for item in data: total = item["base"] + item["bonus"] ws.append([item["name"], item["base"], item["bonus"], total]) wb.save("工资表.xlsx")
九、总结
| 操作 | openpyxl 支持情况 |
|---|---|
| 创建新表格 | ✅ |
| 写入数据 | ✅ |
| 单元格格式设置 | ✅ |
| 图表支持 | ✅(较复杂) |
.xls 格式支持 |
❌(用 xlrd) |
| 大文件读写性能 | 中等 |
https://bigu.wang
https://www.bigu.wang
https://binm.wang
https://www.binm.wang
https://bint.wang
https://www.bint.wang
https://biop.wang
https://www.biop.wang
https://bits.wang
https://www.bits.wang
https://bjqb.wang
https://www.bjqb.wang
https://bjsm.wang
https://www.bjsm.wang
https://bleo.wang
https://www.bleo.wang
https://ono.wang
https://www.ono.wang
https://onz.wang
https://www.onz.wang
https://opo.wang
https://www.opo.wang
https://osm.wang
https://www.osm.wang
https://osn.wang
https://www.osn.wang
https://ovi.wang
https://www.ovi.wang
https://oxq.wang
https://www.oxq.wang
https://oti.wang
https://www.oti.wang
https://owu.wang
https://www.owu.wang
https://piq.wang
https://www.piq.wang
https://qmi.wang
https://www.qmi.wang
https://qki.wang
https://www.qki.wang
https://ref.wang
https://www.ref.wang
https://sak.wang
https://www.sak.wang
https://sar.wang
https://www.sar.wang
https://sfa.wang
https://www.sfa.wang
https://sfe.wang
https://www.sfe.wang
https://sgo.wang
https://www.sgo.wang
https://sku.wang
https://www.sku.wang
https://ycxjz.cn
https://www.ycxjz.cn
https://bnbmhomes.cn
https://www.bnbmhomes.cn
https://jinjianzuche.com
https://www.jinjianzuche.com
https://ahswt.cn
https://www.ahswt.cn
https://szwandaj.cn
https://www.szwandaj.cn
https://psbest.cn
https://www.psbest.cn
https://shanghai-arnold.cn
https://www.shanghai-arnold.cn
https://zgsscw.com
https://www.zgsscw.com
https://shxqth.cn
https://www.shxqth.cn
https://wdxj.cn
https://www.wdxj.cn
https://jad168.com
https://www.jad168.com
https://ultratrailms.cn
https://www.ultratrailms.cn
https://tztsjd.cn
https://www.tztsjd.cn
https://csqcbx.cn
https://www.csqcbx.cn
https://qazit.cn
https://www.qazit.cn
https://ahzjyl.cn
https://www.ahzjyl.cn
更多推荐


所有评论(0)