刘博平的博客

一个软件工程师

Python读写Excel

openpyxl

文档

https://openpyxl.readthedocs.io/en/default/

安装

pip install openpyxl

示例代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
from openpyxl import Workbook
wb = Workbook()

# grab the active worksheet
ws = wb.active

# Data can be assigned directly to cells
ws['A1'] = 42

# Rows can also be appended
ws.append([1, 2, 3])

# Python types will automatically be converted
import datetime
ws['A2'] = datetime.datetime.now()

# Save the file
wb.save("sample.xlsx")