I am trying to remove a sheet in work book but get a Error, i notice that i have to manually open that file save it and then it load correctly.
from openpyxl import load_workbookdef delete_sheet_by_name(file_path, sheet_name): wb = load_workbook(file_path) if sheet_name in wb.sheetnames: sheet = wb[sheet_name] wb.remove(sheet) wb.save(file_path) print(f"The sheet '{sheet_name}' has been deleted.") else: print(f"The sheet '{sheet_name}' does not exist in the workbook.")file_path = r'E:\Projects\Python\Delete Sheet\1.xlsx'sheet_name_to_delete = 'Evaluation Warning'delete_sheet_by_name(file_path, sheet_name_to_delete)
File "E:\Projects\Python\Delete Sheet\pandas_solution.py", line 18, in <module> delete_sheet_by_name(file_path, sheet_name_to_delete) File "E:\Projects\Python\Delete Sheet\pandas_solution.py", line 5, in delete_sheet_by_name wb = load_workbook(file_path) ^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Skynet\anaconda3\Lib\site-packages\openpyxl\reader\excel.py", line 346, in load_workbook reader.read() File "C:\Users\Skynet\anaconda3\Lib\site-packages\openpyxl\reader\excel.py", line 301, in read self.read_worksheets() File "C:\Users\Skynet\anaconda3\Lib\site-packages\openpyxl\reader\excel.py", line 237, in read_worksheets ws_parser.bind_all() File "C:\Users\Skynet\anaconda3\Lib\site-packages\openpyxl\worksheet\_reader.py", line 465, in bind_all self.bind_cells() File "C:\Users\Skynet\anaconda3\Lib\site-packages\openpyxl\worksheet\_reader.py", line 368, in bind_cells for idx, row in self.parser.parse(): File "C:\Users\Skynet\anaconda3\Lib\site-packages\openpyxl\worksheet\_reader.py", line 163, in parse obj = prop[1].from_tree(element) ^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Skynet\anaconda3\Lib\site-packages\openpyxl\descriptors\serialisable.py", line 103, in from_tree return cls(**attrib) ^^^^^^^^^^^^^TypeError: SheetFormatProperties.__init__() got an unexpected keyword argument 'defaultColWidthPt'