我已经将openpyxl与Workbook(write_only = True)一起使用来创建大型Excel xlsx文件。在此模式下,我无法格式化Excel标头。然后我保存xlsx并使用openpyxl load_workbook(my_book)再次打开它,并能够格式化单元格。然后我保存文件。如果文件不是太大,则会保存,但会产生内存错误。 所以Openfile允许我创建并保存工作表,但不一定要重新打开并保存相同的工作表。 在这个例子中,我只是load_workbook然后保存而不更改xlsx以显示错误
from openpyxl import Workbook
from openpyxl import load_workbook
from openpyxl.styles import Font
from openpyxl.styles import PatternFill
wb = Workbook(write_only=True)
# then append a lot of rows
logging.info('Save unformatted xlsx')
wb.save(my_book)
workbook = load_workbook(my_book)
# the worksheet always loads ok at this point, even when 700,000 rows
workbook.save(my_book)
## Immediately after load_workbook I do workbook.save(my_book).
When the rowcount is around 8600 there is no problem. When 350,000 there is a memory error
File "src\lxml\serializer.pxi", line 1268, in lxml.etree._IncrementalFileWriter._handle_error
File "src\lxml\etree.pyx", line 316, in lxml.etree._ExceptionContext._raise_if_stored
File "src\lxml\serializer.pxi", line 650, in lxml.etree._FilelikeWriter.write
MemoryError
logging.info('Saved unformatted xlsx immediately after opening again')
workbook = load_workbook(my_book)
# If no error I do some formatting and all is well and can save ok
Python 3.4.3 openpyxl(2.5.1) lxml(4.2.1)
旧版本有很多解决方案,但我看不到openpyxl(2.5.1)的任何解决方案。 有没有人有openpyxl的答案,或者可以推荐使用什么来打开现有的大型xlsx和格式化单元格?
答案 0 :(得分:0)
这不是主题,即使只是内存错误也是错误的。无论如何,最后我只是导出到CSV而不是XSLX,因为Excel仍然很好地打开它,不值得打扰。