我正在创建一个生成格式化Excel电子表格的脚本。我正在尝试使用[工作表] .column_dimensions []设置自定义列宽。问题是,当我使用此代码包含任何行时,脚本将无法再保存工作簿。如果我注释掉保存文件行并保留列宽行,则脚本不会产生错误。如果我注释掉列宽行并保留保存文件行,则同样如此。
以下是代码:(注意:出于测试目的,除了一个列宽行之外的所有行都已被注释掉。)
# Import libraries used by this script and set the Workbook
from openpyxl import Workbook
from openpyxl.styles import PatternFill, Border, Side, Alignment, Protection, Font, Color
from openpyxl.styles import NamedStyle, Font, Border, Color
wb = Workbook()
# Set font styles to be used in the workbook
default = NamedStyle(name="default")
default.font = Font(name='Arial', size=10, color="000000")
title = NamedStyle(name="title")
title.font = Font(name='Arial', size=14, color="000000", bold=True)
header = NamedStyle(name="header")
header.font = Font(name='Arial', size=11, color="BFBFBF", bold=True)
bd = Side(style='thin', color="000000")
header.border = Border(left=bd, top=bd, right=bd, bottom=bd)
trend = NamedStyle(name="trend")
trend.font = Font(name='Wingdings', size=11)
wb.add_named_style(default)
wb.add_named_style(title)
wb.add_named_style(header)
wb.add_named_style(trend)
# Declare file name and path for the workbook
dest_filename = 'test-book.xlsx'
# Create and Format Summary tab
ws1 = wb.active
ws1.title = 'Summary of Results'
ws1.column_dimensions['A'] = 18.00
#ws1.column_dimensions['B'] = 26.00
#ws1.column_dimensions['C'] = 9.00
#ws1.column_dimensions['D'] = 9.00
#ws1.column_dimensions['E'] = 9.00
#ws1.column_dimensions['F'] = 9.00
#ws1.column_dimensions['G'] = 9.00
# Set Summary tab Title
ws1.merge_cells('A1:G1')
ws1['A1'].style = title
ws1['A1'] = 'Summary of Results'
# Create and Format Detailed tab
ws2 = wb.create_sheet(title='Detailed Results')
#ws2.column_dimensions['A'] = 18.00
#ws2.column_dimensions['B'] = 26.00
#ws2.column_dimensions['C'] = 7.00
#ws2.column_dimensions['D'] = 85.00
#ws2.column_dimensions['E'] = 25.00
#ws2.column_dimensions['F'] = 45.00
# Set Detailed tab Title
ws2.merge_cells('A1:F1')
ws2['A1'].style = title
ws2['A1'] = 'Detailed Results'
# Save the Workbook to the specified file name and path
wb.save(filename = dest_filename)
并且,这是结果错误:
Traceback (most recent call last):
File "generate-report.py", line 68, in <module>
wb.save(filename = dest_filename)
File "/usr/local/lib/python2.7/dist- packages/openpyxl/workbook/workbook.py", line 339, in save
save_workbook(self, filename)
File "/usr/local/lib/python2.7/dist-packages/openpyxl/writer/excel.py", line 268, in save_workbook
writer.save(filename)
File "/usr/local/lib/python2.7/dist-packages/openpyxl/writer/excel.py", line 250, in save
self.write_data()
File "/usr/local/lib/python2.7/dist-packages/openpyxl/writer/excel.py", line 81, in write_data
self._write_worksheets()
File "/usr/local/lib/python2.7/dist-packages/openpyxl/writer/excel.py", line 199, in _write_worksheets
xml = ws._write()
File "/usr/local/lib/python2.7/dist- packages/openpyxl/worksheet/worksheet.py", line 866, in _write
return write_worksheet(self)
File "/usr/local/lib/python2.7/dist- packages/openpyxl/writer/worksheet.py", line 100, in write_worksheet
cols = ws.column_dimensions.to_tree()
File "/usr/local/lib/python2.7/dist-packages/openpyxl/worksheet/dimensions.py", line 229, in to_tree
for col in sorted(self.values(), key=sorter):
File "/usr/local/lib/python2.7/dist- packages/openpyxl/worksheet/dimensions.py", line 222, in sorter
value.reindex()
AttributeError: 'float' object has no attribute 'reindex'
我发现人们有类似的问题,修复是将OpenPyXl升级到1.8或1.9,但我目前正在运行2.4.1版本
非常感谢任何帮助。
答案 0 :(得分:1)
ws1.column_dimensions['A']
会返回ColumDimension
个对象。你需要设置它的宽度。
答案 1 :(得分:0)
ws.column_dimensions['A'].width = 40.0