python的openpyxl模块的workbook.save()无法正常工作

时间:2015-03-25 17:10:35

标签: python xlsx openpyxl import-from-excel

我试图使用openpyxl模块打开XLSX文件。以下是我的剧本: -

from openpyxl import load_workbook

print "Going to execute the script"
workbook = load_workbook("FileName.xlsx")
worksheet = workbook.get_sheet_by_name("Sheet01")
worksheet['B8'] = "Customer07"
workbook.save("FileName.xlsx")
print "End of script execution"

当我执行此脚本时,我收到以下错误: -

/usr/lib/python2.6/site-packages/openpyxl-2.2.0-py2.6.egg/openpyxl/xml/__init__.py:15: UserWarning: The installed version of lxml is too old to be used with openpyxl
  warnings.warn("The installed version of lxml is too old to be used with openpyxl")
Going to execute the script
/usr/lib/python2.6/site-packages/openpyxl-2.2.0-py2.6.egg/openpyxl/workbook/names/named_range.py:121: UserWarning: Discarded range with reserved name
  warnings.warn("Discarded range with reserved name")
Traceback (most recent call last):
  File "check.py", line 11, in <module>
    workbook.save("FileName.xlsx")
  File "/usr/lib/python2.6/site-packages/openpyxl-2.2.0-py2.6.egg/openpyxl/workbook/workbook.py", line 296, in save
    save_workbook(self, filename)
  File "/usr/lib/python2.6/site-packages/openpyxl-2.2.0-py2.6.egg/openpyxl/writer/excel.py", line 191, in save_workbook
    writer.save(filename, as_template=as_template)
  File "/usr/lib/python2.6/site-packages/openpyxl-2.2.0-py2.6.egg/openpyxl/writer/excel.py", line 174, in save
    self.write_data(archive, as_template=as_template)
  File "/usr/lib/python2.6/site-packages/openpyxl-2.2.0-py2.6.egg/openpyxl/writer/excel.py", line 85, in write_data
    self._write_worksheets(archive)
  File "/usr/lib/python2.6/site-packages/openpyxl-2.2.0-py2.6.egg/openpyxl/writer/excel.py", line 111, in _write_worksheets
    write_worksheet(sheet, self.workbook.shared_strings,
  File "/usr/lib/python2.6/site-packages/openpyxl-2.2.0-py2.6.egg/openpyxl/writer/worksheet.py", line 299, in write_worksheet
    xf.write(comments)
  File "/usr/lib/python2.6/contextlib.py", line 34, in __exit__
    self.gen.throw(type, value, traceback)
  File "/usr/lib/python2.6/site-packages/openpyxl-2.2.0-py2.6.egg/openpyxl/xml/xmlfile.py", line 42, in element
    yield
  File "/usr/lib/python2.6/site-packages/openpyxl-2.2.0-py2.6.egg/openpyxl/writer/worksheet.py", line 276, in write_worksheet
    hf = write_header_footer(worksheet)
  File "/usr/lib/python2.6/site-packages/openpyxl-2.2.0-py2.6.egg/openpyxl/writer/worksheet.py", line 172, in write_header_footer
    header = worksheet.header_footer.getHeader()
  File "/usr/lib/python2.6/site-packages/openpyxl-2.2.0-py2.6.egg/openpyxl/worksheet/header_footer.py", line 150, in getHeader
    t.append(self.left_header.get())
  File "/usr/lib/python2.6/site-packages/openpyxl-2.2.0-py2.6.egg/openpyxl/worksheet/header_footer.py", line 90, in get
    t.append('&%d' % self.font_size)
TypeError: %d format: a number is required, not str

此后,当我打开&#34; FileName.xlsx&#34;时,弹出框中显示消息&#34; Excel在&#39; FileName.xlsx&#39;中找到了不可读的评论。你想要恢复这个工作簿的内容吗?如果您信任此工作簿的来源,请单击是&#34; 。无论我点击什么按钮,工作簿都是空的。

如果我在脚本中评论以下行,

workbook.save("FileName.xlsx")

脚本运行时没有任何错误,但修改不保存在工作簿中。 请帮帮我。

2 个答案:

答案 0 :(得分:0)

我昨晚遇到了同样的问题,我认为我的Excel文件没有标题,但实际上它确实有一个页眉和页脚(当你按“打印预览”时可能会看到)< / p>

无论如何,这是我的hacky修复:

我注释掉了这行代码并添加了pass

"<path-to-library>/openpyxl/worksheet/header_footer.py", line 90, in get
Line 89-91 below

if self.font_size:
    # t.append('&%d' % self.font_size) # commented out this line
    pass # added this line "pass"

此“修复”只会影响页眉和页脚的font_size(如果你问我,这不是特别大的交易)。但是您可能会发现您希望将这些页眉和页脚存储到变量中,以便您可以在程序的未来以其他方式使用它们。

如果是这种情况,我在下面提供了一些代码,将这些页眉和页脚提取到相应的词典中(如果存在的话)。

注意:这可能也有助于确定您的文档是否有页眉和页脚。

wb.save(<path/to/file>)之后:

header_dict = {}
if ws.header_footer.hasHeader():
    header_dict['center'] = ws.header_footer.center_header.text
    header_dict['left'] = ws.header_footer.left_header.text
    header_dict['right'] = ws.header_footer.right_header.text

footer_dict = {}
if ws.header_footer.hasFooter():
    footer_dict['center'] = ws.header_footer.center_footer.text
    footer_dict['left'] = ws.header_footer.left_footer.text
    footer_dict['right'] = ws.header_footer.right_footer.text

return header_dict, footer_dict

此header_footer模块可能提供了一种从Excel文件中删除这些文件的方法,但可以在文档中找到更多帮助OpenPyXL - Headers and Footers

答案 1 :(得分:-1)

尝试推杆         工作表['B8']。value =“Customer07” 代替         工作表['B8'] =“Customer07”