使用xlwt写入现有的工作簿

我无法findxlwt用于写入现有文件的示例。 我有一个现有的xls文件,我需要写入。 当我使用xlrd来读取文件时,我似乎无法弄清楚如何将返回到“xlwt.Workbook”的“Book”types转换。 如果有人能指点我一个例子,我将不胜感激。

我无法findxlwt用于写入现有文件的示例。

没有例子。 这不可能。 不用xlwt,也不用其他软件。 XLS文件结构非常复杂,并不像数据库那样可以在您select的表中追加行。

无论使用什么软件,都需要像用户一样使用Excel和键盘:(1)“打开文件”,即将内容加载到内存中(2)操作内存信息(3)“保存”(其中吹掉现有文件并用新文件replace)或“另存为”(写入新文件并保持现有文件不变)。

我在 12个小时前就告诉过你了,但是这里又是这样:

访问此摘要网站 。

兴趣点:

  1. xlutils包

  2. xlrd,xlwt和xlutils教程… 包含示例

  3. 谷歌组/邮件列表提出这样的问题(帮助先完成了教程)

以下是我最近使用的一些示例代码。

它打开一个工作簿,沿着行,如果条件满足,它会在行中写入一些数据。 最后它保存修改后的文件。

from xlutils.copy import copy # http://pypi.python.org/pypi/xlutils from xlrd import open_workbook # http://pypi.python.org/pypi/xlrd from xlwt import easyxf # http://pypi.python.org/pypi/xlwt START_ROW = 297 # 0 based (subtract 1 from excel row number) col_age_november = 1 col_summer1 = 2 col_fall1 = 3 rb = open_workbook(file_path,formatting_info=True) r_sheet = rb.sheet_by_index(0) # read only copy to introspect the file wb = copy(rb) # a writable copy (I can't read values out of this, only write to it) w_sheet = wb.get_sheet(0) # the sheet to write to within the writable copy for row_index in range(START_ROW, r_sheet.nrows): age_nov = r_sheet.cell(row_index, col_age_november).value if age_nov == 3: #If 3, then Combo I 3-4 year old for both summer1 and fall1 w_sheet.write(row_index, col_summer1, 'Combo I 3-4 year old') w_sheet.write(row_index, col_fall1, 'Combo I 3-4 year old') wb.save(file_path + '.out' + os.path.splitext(file_path)[-1]) 

你需要xlutils.copy 。 尝试这样的事情:

 from xlutils.copy import copy w = copy('book1.xls') w.get_sheet(0).write(0,0,"foo") w.save('book2.xls') 

请记住,在这个问题中 ,默认情况下不能覆盖单元格。

代码示例正是这样的:

 from xlutils.copy import copy from xlrd import * w = copy(open_workbook('book1.xls')) w.get_sheet(0).write(0,0,"foo") w.save('book2.xls') 

你需要创buildbook1.xls来testing,但你明白了。

我有同样的问题。 我的客户命令我更新XLS(而不是XLSX)Excel文件的Python 3.4脚本。

第一个软件包xlrd是通过“pip install”安装的,在我的Python家中没有问题。

第二个xlwt需要说“pip install xlwt-future”兼容。

第三个xlutils不支持Python 3,但是我适应了它,现在它至less可以用于虚拟脚本:

 #!C:\Python343\python from xlutils.copy import copy # http://pypi.python.org/pypi/xlutils from xlrd import open_workbook # http://pypi.python.org/pypi/xlrd from xlwt import easyxf # http://pypi.python.org/pypi/xlwt file_path = 'C:\Dev\Test_upd.xls' rb = open_workbook('C:\Dev\Test.xls',formatting_info=True) r_sheet = rb.sheet_by_index(0) # read only copy to introspect the file wb = copy(rb) # a writable copy (I can't read values out of this, only write to it) w_sheet = wb.get_sheet(0) # the sheet to write to within the writable copy w_sheet.write(1, 1, 'Value') wb.save(file_path) 

我在这里附上了这个文件: http : //ifolder.su/43507580

写到alexander.samoylov@gmail.com如果过期。

PS:有些函数不是在虚拟示例中调用的,所以也许他们也需要进行适配。 谁想要这样做,通过谷歌帮助一个一个的修复例外。 这不是一个非常困难的任务,因为包代码很小…

我openpyxl

 # -*- coding: utf-8 -*- import openpyxl file = 'sample.xlsx' wb = openpyxl.load_workbook(filename=file) # Seleciono la Hoja ws = wb.get_sheet_by_name('Hoja1') # Valores a Insertar ws['A3'] = 42 ws['A4'] = 142 # Escribirmos en el Fichero wb.save(file)