Pyker42
Pyker42

Reputation: 11

OpenPyXl Not Saving File with Custom Column Widths

I am making a script that will generate a formatted Excel Spreadsheet. I am trying to set custom column widths using [worksheet].column_dimensions[]. The problem is that when I include any lines with this code, the script no longer is able to save the Workbook. If I comment out the save file line and leave the column width lines, the script produces no errors. Same thing if I comment out the column width lines and leave the save file line.

Here is the code: (Note: for testing purposes, all but one of the column width lines has been commented out.)

# 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)

And, here is the resulting error:

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'

I found that people had similar issues and that the fix was to upgrade OpenPyXl to 1.8 or 1.9, but I am currently running version 2.4.1

Any help would be greatly appreciated.

Upvotes: 1

Views: 1317

Answers (2)

Berzin Gregory
Berzin Gregory

Reputation: 21

ws.column_dimensions['A'].width = 40.0

Upvotes: 0

Charlie Clark
Charlie Clark

Reputation: 19507

ws1.column_dimensions['A'] returns a ColumDimension object. You need to set the width of this.

Upvotes: 1

Related Questions