How to resolve Openpyxl TypeError: expected <class 'str'> error in openpyxl-3.0.3
How to resolve Openpyxl TypeError: expected <class 'str'> error in openpyxl-3.0.3
当我从 openpyxl-2.5.12 升级到 openpyxl-3.0.3 时,我开始收到以下错误:
C:\workspace\venv_py37_64\lib\site-packages\openpyxl\descriptors\base.py", line 42, in __set__
raise TypeError('expected ' + str(self.expected_type))
TypeError: expected <class 'str'>
我该如何解决这个问题?我相信它失败了:
adjusted_width = (max_length + 2) * 1.2
worksheet.column_dimensions[column].width = adjusted_width
我目前的分辨率:
我不得不降级到 openpyxl-2.5.12 来暂时解决错误。请让我知道是否有替代解决方案或我需要做哪些更改才能使我的代码与最新的 openpyxl 版本一起使用。谢谢。
这是我的完整代码片段:
def format_excel_file(excel_file_path):
"""
Formats the provided excel file - autosize, colors cell and draws thin borders
:param excel_file_path:
:return:
"""
my_name = 'format_excel_file()'
logger.info("Entered: {}".format(my_name))
excel_file_path = os.path.abspath(excel_file_path)
logger.info("Excel File: {}".format(excel_file_path))
wb = openpyxl.load_workbook(filename=excel_file_path)
fail_fill = PatternFill(start_color='f2a7a7',
end_color='f2a7a7',
fill_type='solid')
warn_fill = PatternFill(start_color='f5da71',
end_color='f5da71',
fill_type='solid')
pass_fill = PatternFill(start_color='8adb8d',
end_color='8adb8d',
fill_type='solid')
header_fill = PatternFill(start_color='46b363',
end_color='46b363',
fill_type='solid')
thin_border = Border(left=Side(style='thin'),
right=Side(style='thin'),
top=Side(style='thin'),
bottom=Side(style='thin'))
for worksheet in wb.worksheets:
for r, row in enumerate(worksheet.rows, start=1):
if r == 1:
for cell in row:
cell.fill = header_fill
for col in worksheet.columns:
max_length = 0
column = col[0].column # Get the column name
for cell in col:
cell.border = thin_border
if 'FAIL' in str(cell.value):
cell.fill = fail_fill
elif 'PASS' in str(cell.value):
cell.fill = pass_fill
elif 'WARN' in str(cell.value):
cell.fill = warn_fill
if cell.coordinate in worksheet.merged_cells: # not check merge_cells
continue
try: # Necessary to avoid error on empty cells
if len(str(cell.value)) > max_length:
max_length = len(cell.value)
except IOError as e:
print(e)
pass
adjusted_width = (max_length + 2) * 1.2
worksheet.column_dimensions[column].width = adjusted_width
wb.save(excel_file_path)
logger.info("Exited: {}".format(my_name))
问题是 2.6.1 中的更改,现在在设置宽度时需要列字母,而不是列号。
幸运的是,更改非常简单。变化
column = col[0].column # Get the column name
至
column = col[0].column_letter # Get the column name
当我从 openpyxl-2.5.12 升级到 openpyxl-3.0.3 时,我开始收到以下错误:
C:\workspace\venv_py37_64\lib\site-packages\openpyxl\descriptors\base.py", line 42, in __set__
raise TypeError('expected ' + str(self.expected_type))
TypeError: expected <class 'str'>
我该如何解决这个问题?我相信它失败了:
adjusted_width = (max_length + 2) * 1.2
worksheet.column_dimensions[column].width = adjusted_width
我目前的分辨率: 我不得不降级到 openpyxl-2.5.12 来暂时解决错误。请让我知道是否有替代解决方案或我需要做哪些更改才能使我的代码与最新的 openpyxl 版本一起使用。谢谢。
这是我的完整代码片段:
def format_excel_file(excel_file_path):
"""
Formats the provided excel file - autosize, colors cell and draws thin borders
:param excel_file_path:
:return:
"""
my_name = 'format_excel_file()'
logger.info("Entered: {}".format(my_name))
excel_file_path = os.path.abspath(excel_file_path)
logger.info("Excel File: {}".format(excel_file_path))
wb = openpyxl.load_workbook(filename=excel_file_path)
fail_fill = PatternFill(start_color='f2a7a7',
end_color='f2a7a7',
fill_type='solid')
warn_fill = PatternFill(start_color='f5da71',
end_color='f5da71',
fill_type='solid')
pass_fill = PatternFill(start_color='8adb8d',
end_color='8adb8d',
fill_type='solid')
header_fill = PatternFill(start_color='46b363',
end_color='46b363',
fill_type='solid')
thin_border = Border(left=Side(style='thin'),
right=Side(style='thin'),
top=Side(style='thin'),
bottom=Side(style='thin'))
for worksheet in wb.worksheets:
for r, row in enumerate(worksheet.rows, start=1):
if r == 1:
for cell in row:
cell.fill = header_fill
for col in worksheet.columns:
max_length = 0
column = col[0].column # Get the column name
for cell in col:
cell.border = thin_border
if 'FAIL' in str(cell.value):
cell.fill = fail_fill
elif 'PASS' in str(cell.value):
cell.fill = pass_fill
elif 'WARN' in str(cell.value):
cell.fill = warn_fill
if cell.coordinate in worksheet.merged_cells: # not check merge_cells
continue
try: # Necessary to avoid error on empty cells
if len(str(cell.value)) > max_length:
max_length = len(cell.value)
except IOError as e:
print(e)
pass
adjusted_width = (max_length + 2) * 1.2
worksheet.column_dimensions[column].width = adjusted_width
wb.save(excel_file_path)
logger.info("Exited: {}".format(my_name))
问题是 2.6.1 中的更改,现在在设置宽度时需要列字母,而不是列号。
幸运的是,更改非常简单。变化
column = col[0].column # Get the column name
至
column = col[0].column_letter # Get the column name