首页 > 解决方案 > 如何使用openpyxl更新excel文件中的列表?

问题描述

我有模板 excel 文件。我需要将目录中的文件名列表更新为 Excel 文件。像这样: 在此处输入图像描述

这是我的代码:

import xlsxwriter 
import os
import datetime
import openpyxl
import pathlib
import xlsxwriter
arr = os.listdir('C:/Users/11359023/Desktop/source')
xfile = openpyxl.load_workbook('C:/Users/11359023/Desktop/vee_report.xlsx')
sheet = xfile.get_sheet_by_name('Property Files Report')
x=0
col = "A"
row = x
for f in arr:
    sheet.write(row,col,f)
    row += 1
xfile.save('vee_report.xlsx')

当我运行此代码时。它不会更新到我的 excel 文件并得到错误“工作表”对象没有属性“写入”。请告诉我如何解决这个问题。

标签: python

解决方案


您应该在工作表中设置单元格的值,如下所示。

x = 1 # not x = 0
...
sheet['{0}{1}'.format(col, row)].value = f

希望它可以帮助你。


推荐阅读