Excel file is being corrupted when trying to modify 1 cell using openpyxl python

Viewed 627

I am trying to change 1 cell in a macro enabled excel 2013 file using openpyxl. This should be a relatively easy task I have the following code it works but for some reason when I later try to open the modified excel file I get the error message from excel.

" We found a problem with some contents in "workbook" do you want to recover?"

if I recover the file it removes a lot of content from the file but I can still see that the only cell I wanted to modify has the correct data.

when I open the error log I get the following message:

<?xml version="1.0" encoding="UTF-8" standalone="true"?>
-<recoveryLog xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
<logFileName>error086600_01.xml</logFileName>
<summary>Errors were detected in file 'C:\Files\2018 Workflow\Stress Due Date (NA) --- TR9999999 Pckg#9 - Emergency Equipment - 9 - 9 - 9\Panel Analysis --- 9 Version A.xlsm'</summary>
-<repairedParts summary="Following is a list of repairs:">
<repairedPart xml:space="preserve">Repaired Part: /xl/worksheets/sheet1.xml part. </repairedPart>
 </repairedParts>
 </recoveryLog>

The following is my python code, I have looked through the documentation and it seems like it should be working but why is it corrupting my files?

""" 
The next part of the script will modify the stress panel excel documents 
"""    
wb = load_workbook(filename = os.path.join(new_path,new_panel + excel_ext), read_only = False, keep_vba = True) 
ws = wb.get_sheet_by_name("Adapter_Panel_Tool")
ws['C2'] = "Adapter Panel Quick Stress Analyses Tool - " +  packet_list['Installations list'][0]
wb.save(os.path.join(new_path,new_panel  + " Version A" + excel_ext))
wb.close()


#checks if there are 2 files: the filled one and the empty one and deletes the empty if it exist. 
is_file_filled_out = os.path.isfile(os.path.join(new_path,new_panel + " Version A" +  excel_ext))
is_there_empty_file = os.path.isfile(os.path.join(new_path, new_panel + excel_ext))
if is_file_filled_out == True and is_there_empty_file == True:
    os.remove(os.path.join(new_path,new_panel + excel_ext))
    print("\n\n The following file will be remove: \n" + new_panel + excel_ext + "\n\n" )
    print("The prefilled template has been created: \n " + new_path + new_panel + excel_ext)
0 Answers
Related