Fixed issue with variables not being updated in Excel file
This commit is contained in:
@@ -8,6 +8,9 @@ def update_excel_with_direct_xml(excel_path, store_name):
|
||||
"""
|
||||
Update all references to {store_name} in the Excel file by directly modifying XML
|
||||
|
||||
This function preserves all cell values and formulas that were previously set
|
||||
by the update_excel_variables function.
|
||||
|
||||
Args:
|
||||
excel_path: Path to the Excel file
|
||||
store_name: The store name to replace {store_name} with
|
||||
@@ -39,26 +42,30 @@ def update_excel_with_direct_xml(excel_path, store_name):
|
||||
|
||||
# Only modify XML files that might contain formulas or text
|
||||
if item.filename.endswith('.xml'):
|
||||
# Convert to string for text replacement
|
||||
try:
|
||||
text_content = content.decode('utf-8')
|
||||
|
||||
# Check if this file contains our placeholder
|
||||
if '{store_name}' in text_content:
|
||||
# Count occurrences before replacement
|
||||
occurrences = text_content.count('{store_name}')
|
||||
total_replacements += occurrences
|
||||
# Skip sheet8.xml which is the Variables sheet (based on common Excel structure)
|
||||
if 'sheet8.xml' in item.filename:
|
||||
print(f"Skipping Variables sheet: {item.filename}")
|
||||
else:
|
||||
# Convert to string for text replacement
|
||||
try:
|
||||
text_content = content.decode('utf-8')
|
||||
|
||||
# Replace all instances of {store_name} with the actual store name
|
||||
modified_content = text_content.replace('{store_name}', store_name)
|
||||
|
||||
# Convert back to bytes
|
||||
content = modified_content.encode('utf-8')
|
||||
|
||||
print(f"Replaced {occurrences} instances of '{{store_name}}' in {item.filename}")
|
||||
except UnicodeDecodeError:
|
||||
# Not a text file, leave as is
|
||||
pass
|
||||
# Check if this file contains our placeholder
|
||||
if '{store_name}' in text_content:
|
||||
# Count occurrences before replacement
|
||||
occurrences = text_content.count('{store_name}')
|
||||
total_replacements += occurrences
|
||||
|
||||
# Replace all instances of {store_name} with the actual store name
|
||||
modified_content = text_content.replace('{store_name}', store_name)
|
||||
|
||||
# Convert back to bytes
|
||||
content = modified_content.encode('utf-8')
|
||||
|
||||
print(f"Replaced {occurrences} instances of '{{store_name}}' in {item.filename}")
|
||||
except UnicodeDecodeError:
|
||||
# Not a text file, leave as is
|
||||
pass
|
||||
|
||||
# Write the file (original or modified) to the new zip
|
||||
zip_out.writestr(item, content)
|
||||
|
||||
Reference in New Issue
Block a user