共计 380 个字符,预计需要花费 1 分钟才能阅读完成。
from openpyxl import workbook
# excel 文件 & sheet
wb = workbook.Workbook()
sheet = wb.worksheets[0]
# sheet = wb.active
# 找到单元格 (行,列),写入值
cell = sheet.cell(1, 1)
cell.value = '李小龙'
wb.save('info.xlsx')
读取 Excel
from openpyxl import load_workbook
wb = load_workbook('abc.xlsx')
sheet = wb['Sheet1']
# sheet.rows 返回一个生成器对象,用推导列表把对象取出来放 list 里
rows = [x for x in sheet.rows]
# 从第二行开始取值
for i in rows[1:]:
print(i[0].value, i[1].value, i[2].value)
正文完