OpenPyXL Notes
OpenPyXL 是一个用于读写 excel 文件的 Python 库,简单记录一下使用方法。
写入のdemo
from openpyxl import Workbook
from openpyxl.styles import PatternFill
wb = Workbook()
ws = wb.active
ws["A1"] = 20 # assign 1
cell = ws["A2"] # assign 2
cell.value = 1994
cell = ws.cell(row=3, column=1) # assign 3
cell.value = "Amoko"
cell = ws["B1"] # assign 4
cell.fill = PatternFill("solid", fgColor="ff0000")
cell = ws["B2"] # assign 5
cell.fill = PatternFill("solid", fgColor="00ff00")
wb.save("0215.xlsx")
读取のdemo
from openpyxl import load_workbook
wb = load_workbook("0215.xlsx")
ws = wb[wb.sheetnames[0]]
print(ws.max_row, ws.max_column)
cell = ws["B1"]
print(repr(cell.fill.start_color.index))
for row in ws.iter_rows():
for cell in row:
print(cell.row, cell.col_idx, cell.value)
Reference
[1] OpenPyXL 官网文档