import openpyxl
def write_file(file_path):
outwb = openpyxl.Workbook() # 打开一个将写的文件
outws = outwb.create_sheet(title="测试", index=0) # 在将写的文件创建sheet
for row in range(1, 70000):
for col in range(1, 4):
outws.cell(row, col).value = row * 2 # 写文件
outwb.save(file_path) # 一定要记得保存
# write_file('./test.xlsx')
def upadta_xlsx(file):
wb = openpyxl.load_workbook(file)
#获取当前活跃的Worksheet
print(wb.active)
sheet = wb.worksheets[0]
#sheet01 = wb['表名'] 也可以
# print(sheet01.title) # 表格的标题,获取这个sheet的名称
#
# print(sheet01.dimensions) # 表格的大小,这里的大小是指含有数据的表格的大小,即:左上角的坐标:右下角的坐标 #A1:D388
#
# print(sheet01.max_row) # 表格的最大行
#
# print(sheet01.min_row) # 表格的最小行
#
# print(sheet01.max_column) # 表格的最大列
#
# print(sheet01.min_column) # 表格的最小列
#print(sheet01.cell(row=1, column=1).value) # value:单元格的值
sheet['D1'] = '第1个值'
sheet['D2'] = '第2个值'
sheet['D3'] = '第3个值'
sheet['D4'] = '第4个值'
sheet['D5'] = '第5个值'
wb.save(filename=file)
# upadta_xlsx('./test.xlsx')
def updatesheet(file):
wb = openpyxl.load_workbook(file)
#ws1 = wb.create_sheet() # 默认插在最后
#ws2 = wb.create_sheet(0) # 插在开头 ,
# ws.title = "New Title" # 修改表名称
ws2 = wb.create_sheet(title="新sheet1")
ws2['A1']=34654
wb.save(filename=file)
updatesheet('./test.xlsx')
openpyxl读取、更新插入excel
最新推荐文章于 2024-05-14 11:00:23 发布