创建名为d.xlsx文件、用到了其中的工作表【Sheet1】 使用到的库如下:
from openpyxl import Workbook,load_workbook
wb = Workbook()
sheet = wb.active
#保存
wb.save('d.xlsx')
wb = load_workbook('d.xlsx')
sheet = wb['Sheet1']
wb = load_workbook('d.xlsx')
sheet = wb['Sheet1']
#按行查询,范围[A1:C7]
for cell in sheet['A1:C7']:
for c in cell:
print(c.value, end=',')
print()
#按列查询
for cell in sheet.columns:
for c in cell:
print(c.value, end=',')
print()
wb = load_workbook('d.xlsx')
sheet = wb['Sheet1']
#直接填充单元格
sheet['A1']='内容'
#从最后一行的下一行开始追加内容
sheet.append([1,2,3])
wb.save('d.xlsx')
wb = load_workbook('d.xlsx')
sheet = wb['Sheet1']
#直接改单元格
sheet['A1']='内容'
wb.save('d.xlsx')