Когда я обновил 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))