使用 python 写入特定的 Excel 工作表
Write to a specific Excel worksheet using python
我想覆盖已存在的 excel 文件中的特定单元格。我搜索并找到了这个答案,writing to existing workbook using xlwt。我已将其应用如下,
def wrtite_to_excel (self):
#first I must open the specified excel file, sice open_file is in the same class, hence we can get it using self.sheet.
bookwt = copy(self.workbook)
sheetwt= bookwt.get_sheet(0)
#now, I must know the column that was estimated so I overwrite it,
colindex= self.columnBox.current() #returns the index of the estimated column
for i in range (1, self.grid.shape[0]):
if (str (self.sheet.cell_value(i,colindex)).lower () == self.missingBox.get().lower()):
#write the estimated value:
sheetwt.write (i, colindex, self.grid[i])
bookwt.save(self.filename + '.out' + os.path.splitext(self.filename)[-1])
注意,self.workbook已经存在于同一个class的另一个方法中,
def open_file (self, file_name):
try:
self.workbook = xlrd.open_workbook(file_name)
真不知道这是什么意思,'.out' + os.path.splitext(self.filename)[-1],不过貌似导致修改后的文件保存在与原始路径相同但名称不同。
在 运行 程序之后,一个新的 Excel 文件被保存在与原始文件相同的路径中,但是它被保存为一个奇怪的名称 data.xlsx.out.xlsx
而且它没有'打开。我认为是这条线'.out' + os.path.splitext(self.filename)[-1]
造成的。我删除了该行以覆盖原始文件而不保存副本,但是当 运行 程序无法打开原始文件时,我收到一条错误消息,指出该文件无法打开,因为文件格式或扩展名无效。
我真正想要的是修改原始文件而不是创建修改后的副本。
编辑:如果只指定文件名,SiHa 的答案可以修改现有文件而不创建副本,
bookwt.save(self.filename)
而且,它可以通过这种方式保存一个新副本,
filepath, fileext = os.path.splitext(self.filename)
bookwt.save(filepath + '_out' + fileext)
或者作为我在问题中的代码中提供的行。但是,在所有这些方法中都存在相同的问题,即修改文件后无法打开。搜索后发现可以通过将原文件的扩展名从.xlsx改为.xls来解决问题。进行此更改后,问题就解决了。这是我找到解决方案的 link http://www.computing.net/answers/office/the-file-formatfile-extension-is-not-valid/19454.html
谢谢。
您可以将 excel 文件另存为 CSV 文件,这意味着当它们被 python 打开时,它们会以逗号分隔的纯文本形式显示值,例如列中包含地址的电子表格a 到 b 和第 1 到 2 行看起来像这样
A1,B1
A2,B2
这意味着您可以像普通文件一样编辑它们并且excel仍然可以打开它们
解释有问题的行:
(self.filename + '.out'
表示将 .out 连接到原始文件名的末尾。
+ os.path.splitext(self.filename)[-1])
表示将文件名拆分为 ['path', 'extension']
列表,然后将最后一个元素(扩展名)再次连接到末尾。
所以你最终得到 data.xlsx.out.xlsx
您应该可以使用 bookwt.save(self.filename)
,但如果您仍然打开文件进行阅读,您可能 运行 会出错。以与上述类似的方式创建副本可能更安全:
filepath, fileext = os.path.splitext(self.filename)
bookwt.save(filepath + '_out' + fileext)
哪个应该给你data_out.xlsx
我想覆盖已存在的 excel 文件中的特定单元格。我搜索并找到了这个答案,writing to existing workbook using xlwt。我已将其应用如下,
def wrtite_to_excel (self):
#first I must open the specified excel file, sice open_file is in the same class, hence we can get it using self.sheet.
bookwt = copy(self.workbook)
sheetwt= bookwt.get_sheet(0)
#now, I must know the column that was estimated so I overwrite it,
colindex= self.columnBox.current() #returns the index of the estimated column
for i in range (1, self.grid.shape[0]):
if (str (self.sheet.cell_value(i,colindex)).lower () == self.missingBox.get().lower()):
#write the estimated value:
sheetwt.write (i, colindex, self.grid[i])
bookwt.save(self.filename + '.out' + os.path.splitext(self.filename)[-1])
注意,self.workbook已经存在于同一个class的另一个方法中,
def open_file (self, file_name):
try:
self.workbook = xlrd.open_workbook(file_name)
真不知道这是什么意思,'.out' + os.path.splitext(self.filename)[-1],不过貌似导致修改后的文件保存在与原始路径相同但名称不同。
在 运行 程序之后,一个新的 Excel 文件被保存在与原始文件相同的路径中,但是它被保存为一个奇怪的名称 data.xlsx.out.xlsx
而且它没有'打开。我认为是这条线'.out' + os.path.splitext(self.filename)[-1]
造成的。我删除了该行以覆盖原始文件而不保存副本,但是当 运行 程序无法打开原始文件时,我收到一条错误消息,指出该文件无法打开,因为文件格式或扩展名无效。
我真正想要的是修改原始文件而不是创建修改后的副本。
编辑:如果只指定文件名,SiHa 的答案可以修改现有文件而不创建副本,
bookwt.save(self.filename)
而且,它可以通过这种方式保存一个新副本,
filepath, fileext = os.path.splitext(self.filename)
bookwt.save(filepath + '_out' + fileext)
或者作为我在问题中的代码中提供的行。但是,在所有这些方法中都存在相同的问题,即修改文件后无法打开。搜索后发现可以通过将原文件的扩展名从.xlsx改为.xls来解决问题。进行此更改后,问题就解决了。这是我找到解决方案的 link http://www.computing.net/answers/office/the-file-formatfile-extension-is-not-valid/19454.html
谢谢。
您可以将 excel 文件另存为 CSV 文件,这意味着当它们被 python 打开时,它们会以逗号分隔的纯文本形式显示值,例如列中包含地址的电子表格a 到 b 和第 1 到 2 行看起来像这样
A1,B1
A2,B2
这意味着您可以像普通文件一样编辑它们并且excel仍然可以打开它们
解释有问题的行:
(self.filename + '.out'
表示将 .out 连接到原始文件名的末尾。
+ os.path.splitext(self.filename)[-1])
表示将文件名拆分为 ['path', 'extension']
列表,然后将最后一个元素(扩展名)再次连接到末尾。
所以你最终得到 data.xlsx.out.xlsx
您应该可以使用 bookwt.save(self.filename)
,但如果您仍然打开文件进行阅读,您可能 运行 会出错。以与上述类似的方式创建副本可能更安全:
filepath, fileext = os.path.splitext(self.filename)
bookwt.save(filepath + '_out' + fileext)
哪个应该给你data_out.xlsx