Fix Excel corruption issue when modifying XML content
This commit is contained in:
@@ -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,46 +257,50 @@ 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)
|
||||||
|
|
||||||
# Only modify XML files that might contain formulas or text
|
# Modify the content
|
||||||
if item.filename.endswith('.xml'):
|
for filename, (content, item) in files_data.items():
|
||||||
# Skip sheet8.xml which is the Variables sheet (based on common Excel structure)
|
# Only modify XML files that might contain formulas or text
|
||||||
if 'sheet8.xml' in item.filename:
|
if filename.endswith('.xml') or filename.endswith('.rels'):
|
||||||
print(f"Skipping Variables sheet: {item.filename}")
|
# Skip sheet8.xml which is the Variables sheet (based on common Excel structure)
|
||||||
else:
|
if 'sheet8.xml' in filename:
|
||||||
# Convert to string for text replacement
|
print(f"Skipping Variables sheet: {filename}")
|
||||||
try:
|
continue
|
||||||
text_content = content.decode('utf-8')
|
|
||||||
|
|
||||||
# Check if this file contains our placeholder
|
# Convert to string for text replacement
|
||||||
if '{store_name}' in text_content:
|
try:
|
||||||
# Count occurrences before replacement
|
text_content = content.decode('utf-8')
|
||||||
occurrences = text_content.count('{store_name}')
|
|
||||||
total_replacements += occurrences
|
|
||||||
|
|
||||||
# Replace all instances of {store_name} with the actual store name
|
# Check if this file contains our placeholder
|
||||||
modified_content = text_content.replace('{store_name}', store_name)
|
if '{store_name}' in text_content:
|
||||||
|
# Count occurrences before replacement
|
||||||
|
occurrences = text_content.count('{store_name}')
|
||||||
|
total_replacements += occurrences
|
||||||
|
|
||||||
# Convert back to bytes
|
# Replace all instances of {store_name} with the actual store name
|
||||||
content = modified_content.encode('utf-8')
|
modified_content = text_content.replace('{store_name}', store_name)
|
||||||
|
|
||||||
print(f"Replaced {occurrences} instances of '{{store_name}}' in {item.filename}")
|
# Convert back to bytes
|
||||||
except UnicodeDecodeError:
|
files_data[filename] = (modified_content.encode('utf-8'), item)
|
||||||
# Not a text file, leave as is
|
|
||||||
pass
|
|
||||||
|
|
||||||
# Write the file (original or modified) to the new zip
|
print(f"Replaced {occurrences} instances of '{{store_name}}' in {filename}")
|
||||||
zip_out.writestr(item, content)
|
except UnicodeDecodeError:
|
||||||
|
# Not a text file, leave as is
|
||||||
|
pass
|
||||||
|
|
||||||
# Clean up the temporary file
|
# Write the modified zip file
|
||||||
if os.path.exists(temp_file):
|
with ZipFile(temp_file, 'w', ZIP_DEFLATED) as zip_out:
|
||||||
os.remove(temp_file)
|
for filename, (content, item) in files_data.items():
|
||||||
|
zip_out.writestr(filename, content)
|
||||||
|
|
||||||
|
# Replace the original file with the modified one
|
||||||
|
shutil.move(temp_file, excel_path)
|
||||||
|
|
||||||
print(f"Total replacements: {total_replacements}")
|
print(f"Total replacements: {total_replacements}")
|
||||||
return True
|
return True
|
||||||
|
|||||||
Reference in New Issue
Block a user