Fix Excel corruption issue when modifying XML content

This commit is contained in:
denisacirstea
2025-09-12 14:02:53 +03:00
parent 4e18caa8f7
commit 3bb09839ae

View File

@@ -248,7 +248,7 @@ def update_excel_with_direct_xml(excel_path, store_name):
# Create a temporary file for modification # Create a temporary file for modification
temp_dir = os.path.dirname(os.path.abspath(excel_path)) temp_dir = os.path.dirname(os.path.abspath(excel_path))
temp_file = os.path.join(temp_dir, "_temp_for_xml_edit.xlsx") temp_file = os.path.join(temp_dir, f"_temp_{os.path.basename(excel_path)}")
# Make a copy of the original file # Make a copy of the original file
import shutil import shutil
@@ -257,19 +257,22 @@ def update_excel_with_direct_xml(excel_path, store_name):
# Count of replacements # Count of replacements
total_replacements = 0 total_replacements = 0
# Process the Excel file # Process the Excel file - use a safer approach
with ZipFile(temp_file, 'r') as zip_in: # First read all files from the zip
with ZipFile(excel_path, 'w', ZIP_DEFLATED) as zip_out: files_data = {}
# Process each file in the zip with ZipFile(excel_path, 'r') as zip_ref:
for item in zip_in.infolist(): for item in zip_ref.infolist():
content = zip_in.read(item.filename) files_data[item.filename] = (zip_ref.read(item.filename), item)
# Modify the content
for filename, (content, item) in files_data.items():
# Only modify XML files that might contain formulas or text # Only modify XML files that might contain formulas or text
if item.filename.endswith('.xml'): if filename.endswith('.xml') or filename.endswith('.rels'):
# Skip sheet8.xml which is the Variables sheet (based on common Excel structure) # Skip sheet8.xml which is the Variables sheet (based on common Excel structure)
if 'sheet8.xml' in item.filename: if 'sheet8.xml' in filename:
print(f"Skipping Variables sheet: {item.filename}") print(f"Skipping Variables sheet: {filename}")
else: continue
# Convert to string for text replacement # Convert to string for text replacement
try: try:
text_content = content.decode('utf-8') text_content = content.decode('utf-8')
@@ -284,19 +287,20 @@ def update_excel_with_direct_xml(excel_path, store_name):
modified_content = text_content.replace('{store_name}', store_name) modified_content = text_content.replace('{store_name}', store_name)
# Convert back to bytes # Convert back to bytes
content = modified_content.encode('utf-8') files_data[filename] = (modified_content.encode('utf-8'), item)
print(f"Replaced {occurrences} instances of '{{store_name}}' in {item.filename}") print(f"Replaced {occurrences} instances of '{{store_name}}' in {filename}")
except UnicodeDecodeError: except UnicodeDecodeError:
# Not a text file, leave as is # Not a text file, leave as is
pass pass
# Write the file (original or modified) to the new zip # Write the modified zip file
zip_out.writestr(item, content) with ZipFile(temp_file, 'w', ZIP_DEFLATED) as zip_out:
for filename, (content, item) in files_data.items():
zip_out.writestr(filename, content)
# Clean up the temporary file # Replace the original file with the modified one
if os.path.exists(temp_file): shutil.move(temp_file, excel_path)
os.remove(temp_file)
print(f"Total replacements: {total_replacements}") print(f"Total replacements: {total_replacements}")
return True return True