一: 安装依赖库
pip3 install openpyxl
二:创建新文件
from openpyxl import Workbook import time book = Workbook() sheet = book.active sheet['A1'] = 11 sheet['A2'] = 22 now = time.strftime("%x") sheet['A3'] = now book.save("demo.xlsx")
三:通过cell()
多行写入
from openpyxl import Workbook book = Workbook() sheet = book.active sheet.cell(row=2, column=2).value = 2 book.save('demo.xlsx')
四:读取单元格
import openpyxl book = openpyxl.load_workbook('demo.xlsx') sheet = book.active a1 = sheet['A1'] a2 = sheet['A2'] a3 = sheet.cell(row=3, column=1) print(a1.value) print(a2.value) print(a3.value)