I'm building an application with which my company analyzes provided table data.
So far so good, but when I try to save the results to a workbook, I get a
TypeError: '<' not supported between instances of 'str' and 'int'
Does anyone know what to do about it?
this is a code snippet:
from openpyxl import *
from openpyxl.styles import Border, Side, Alignment, Font, PatternFill
from tkinter import *
import tkinter
root = tkinter.Tk()
# Properties of the GUI
root.geometry("120x80")
root.title("TOOL_1")
def create_new_workbook():
global wb
wb = Workbook()
ss = wb.active
ss.sheet_view.showGridLines = False
ss.title = "Report"
# Implement thick line vertically the seperate overview sheet
bottom = Side(border_style='thick', color='000000')
border = Border(bottom=bottom)
col = ss.row_dimensions['23']
col.border = border
ss.merge_cells('B3:E6')
ss['B3'].alignment = Alignment(horizontal='center',
vertical='center')
ss['B3'] = "TOTAL"
total_label = ss['A3']
total_label.font = Font(bold=True, size=24)
ss['B7'] = "ZNr"
ZNr_label = ss['B7']
ZNr_label.font = Font(bold=True)
ss.merge_cells('C7:E7')
ss['B9'] = "DEP"
DEP_label = ss['B9']
DEP_label.font = Font(bold=True)
DEP_label.fill = PatternFill(start_color='9BC2E6',
end_color='9BC2E6',
fill_type='solid')
ss.merge_cells('C9:E9')
deposit_amount = ss['C9']
deposit_amount.fill = PatternFill(start_color='9BC2E6',
end_color='9BC2E6',
fill_type='solid')
wb.save(r"Test1.xlsx")
print("Saved")
button_create_new_workbook = Button(root, text="Create Workbook", command=create_new_workbook)
button_create_new_workbook.pack()
root.mainloop()
Your problem is this line:
col = ss.row_dimensions['23']
row_dimensions
appears to accept either an integer or string input. However, when saving it requires an integer input, not a string. Admittedly, I found this quite hard to find and had to do a deep dive into openpyxl. The function that raises your error is:
def rows(self):
"""Return all rows, and any cells that they contain"""
# order cells by row
rows = defaultdict(list)
for (row, col), cell in sorted(self.ws._cells.items()):
rows[row].append(cell)
# add empty rows if styling has been applied
for row in self.ws.row_dimensions.keys() - rows.keys():
rows[row] = []
return sorted(rows.items())
Using print
to isolate the rows, it gave these rows for your worksheet:
(3, [<Cell 'Report'.A3>, <Cell 'Report'.B3>, <MergedCell 'Report'.C3>, <MergedCell 'Report'.D3>, <MergedCell 'Report'.E3>])
(4, [<MergedCell 'Report'.B4>, <MergedCell 'Report'.C4>, <MergedCell 'Report'.D4>, <MergedCell 'Report'.E4>])
(5, [<MergedCell 'Report'.B5>, <MergedCell 'Report'.C5>, <MergedCell 'Report'.D5>, <MergedCell 'Report'.E5>])
(6, [<MergedCell 'Report'.B6>, <MergedCell 'Report'.C6>, <MergedCell 'Report'.D6>, <MergedCell 'Report'.E6>])
(7, [<Cell 'Report'.B7>, <Cell 'Report'.C7>, <MergedCell 'Report'.D7>, <MergedCell 'Report'.E7>])
(9, [<Cell 'Report'.B9>, <Cell 'Report'.C9>, <MergedCell 'Report'.D9>, <MergedCell 'Report'.E9>])
('23', [])
The last row is appended to the dictionary as it is empty, but has styling applied to it. However, you've input it as a string, which raises this error when trying to sort rows.items()
. It may be unintential that row_dimensions
allows strings. However, you can solve your problem by replacing
col = ss.row_dimensions['23']
with
col = ss.row_dimensions[23]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.