from openpyxl import Workbook from openpyxl.compat importrange from openpyxl.utils import get_column_letter from openpyxl.worksheet.pagebreak import Break, PageBreak
wb = Workbook() ws = wb.active
for row inrange(1, 20): for col inrange(1,30): _ = ws.cell(column=col, row=row, value="{0}".format(get_column_letter(col)))
from openpyxl import Workbook from openpyxl.compat importrange from openpyxl.utils import get_column_letter from openpyxl.worksheet.pagebreak import Break, PageBreak
wb = Workbook() dest_filename = 'empty_book.xlsx'
ws = wb.active for row inrange(1, 20): for col inrange(1,30): _ = ws.cell(column=col, row=row, value="{0}".format(get_column_letter(col)))
Traceback (most recent call last): File "test.py", line 24, in <module> wb.save(filename = dest_filename) File "F:\workspace\python\test_openpyxl\test_openpyxl\lib\site-packages\openpyxl\workbook\workbook.py", line 391, in save save_workbook(self, filename) File "F:\workspace\python\test_openpyxl\test_openpyxl\lib\site-packages\openpyxl\writer\excel.py", line 284, in save_workbook writer.save(filename) File "F:\workspace\python\test_openpyxl\test_openpyxl\lib\site-packages\openpyxl\writer\excel.py", line 266, in save self.write_data() File "F:\workspace\python\test_openpyxl\test_openpyxl\lib\site-packages\openpyxl\writer\excel.py", line 83, in write_data self._write_worksheets() File "F:\workspace\python\test_openpyxl\test_openpyxl\lib\site-packages\openpyxl\writer\excel.py", line 203, in _write_worksheets xml = ws._write() File "F:\workspace\python\test_openpyxl\test_openpyxl\lib\site-packages\openpyxl\worksheet\worksheet.py", line 893, in _write return write_worksheet(self) File "F:\workspace\python\test_openpyxl\test_openpyxl\lib\site-packages\openpyxl\writer\worksheet.py", line 151, in write_worksheet xf.write(ws.page_breaks.to_tree()) AttributeError: 'list' object has no attribute 'to_tree'
# worksheet.py # 省略部分代码 if ws.page_breaks: ifisinstance(ws.page_breaks,list): for page_break_item in ws.page_breaks: xf.write(page_break_item.to_tree()) else: xf.write(ws.page_breaks.to_tree())