gpt4 book ai didi

python - OpenPyXl 不保存具有自定义列宽的文件

转载 作者:太空宇宙 更新时间:2023-11-03 15:54:20 24 4
gpt4 key购买 nike

我正在制作一个脚本来生成格式化的 Excel 电子表格。我正在尝试使用 [worksheet].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 版本

任何帮助将不胜感激。

最佳答案

ws1.column_dimensions['A'] 返回一个 ColumDimension 对象。您需要设置其宽度。

关于python - OpenPyXl 不保存具有自定义列宽的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40940712/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com