Implement Excel file generation with variable injection from config.json

This commit is contained in:
denisacirstea
2025-09-12 10:51:02 +03:00
parent 1b9cb50961
commit 9c0435ddd8
7 changed files with 143 additions and 511 deletions

View File

@@ -6,7 +6,7 @@ import datetime
import re
from pathlib import Path
from dateutil.relativedelta import relativedelta
import openpyxl
from update_excel import update_excel_variables
def create_excel_from_template():
"""
@@ -63,8 +63,14 @@ def create_excel_from_template():
shutil.copy2(template_path, output_path)
print(f"Excel file created successfully: {output_path}")
# Now inject variables from config.json into the Variables sheet
inject_variables(output_path, config)
# Update the Excel file with variables from config.json
print("Updating Excel file with variables from config.json...")
update_result = update_excel_variables(output_path)
if update_result:
print("Excel file updated successfully with variables from config.json")
else:
print("Warning: Failed to update Excel file with variables from config.json")
return True
except Exception as e:
@@ -125,125 +131,5 @@ def calculate_years(starting_date, duration):
print(f"Error calculating years: {e}")
return default_years
def inject_variables(excel_path, config):
"""
Inject variables from config.json into the Variables sheet of the Excel file.
Args:
excel_path (str): Path to the Excel file
config (dict): Configuration data from config.json
"""
try:
# Load the workbook
workbook = openpyxl.load_workbook(excel_path)
# Try to find the Variables sheet
sheet_names = workbook.sheetnames
variables_sheet = None
# Print all sheet names for debugging
print("Available sheets:", sheet_names)
# Look for the Variables sheet by name (case-insensitive)
for sheet_name in sheet_names:
if "variable" in sheet_name.lower():
variables_sheet = workbook[sheet_name]
print(f"Found Variables sheet: '{sheet_name}'")
break
# If Variables sheet not found by name, try the last sheet
if variables_sheet is None and sheet_names:
last_sheet_name = sheet_names[-1]
variables_sheet = workbook[last_sheet_name]
print(f"Using last sheet '{last_sheet_name}' as Variables sheet")
# If still not found, try all sheets and look for specific cell patterns
if variables_sheet is None:
for sheet_name in sheet_names:
sheet = workbook[sheet_name]
# Check if this sheet has a cell B2 with a value
if sheet["B2"].value is not None:
variables_sheet = sheet
print(f"Using sheet '{sheet_name}' as it has data in cell B2")
break
if variables_sheet is None:
print("Warning: Variables sheet not found. No variables were injected.")
return
# Get user data from config
user_data = config.get("user_data", {})
# Map cell references to config values based on the image
cell_mappings = {
"B2": user_data.get("store_name", ""),
"B31": user_data.get("starting_date", ""),
"B32": user_data.get("duration", 36),
"B37": user_data.get("open_days_per_month", 0),
# Convenience store type
"H37": user_data.get("convenience_store_type", {}).get("stores_number", 0),
"C37": user_data.get("convenience_store_type", {}).get("monthly_transactions", 0),
# Convert boolean to 1/0 for has_digital_screens
"I37": 1 if user_data.get("convenience_store_type", {}).get("has_digital_screens", False) else 0,
"J37": user_data.get("convenience_store_type", {}).get("screen_count", 0),
"K37": user_data.get("convenience_store_type", {}).get("screen_percentage", 0),
# Convert boolean to 1/0 for has_in_store_radio
"M37": 1 if user_data.get("convenience_store_type", {}).get("has_in_store_radio", False) else 0,
"N37": user_data.get("convenience_store_type", {}).get("radio_percentage", 0),
# Supermarket store type
"H38": user_data.get("supermarket_store_type", {}).get("stores_number", 0),
"C38": user_data.get("supermarket_store_type", {}).get("monthly_transactions", 0),
# Convert boolean to 1/0 for has_digital_screens
"I38": 1 if user_data.get("supermarket_store_type", {}).get("has_digital_screens", False) else 0,
"J38": user_data.get("supermarket_store_type", {}).get("screen_count", 0),
"K38": user_data.get("supermarket_store_type", {}).get("screen_percentage", 0),
# Convert boolean to 1/0 for has_in_store_radio
"M38": 1 if user_data.get("supermarket_store_type", {}).get("has_in_store_radio", False) else 0,
"N38": user_data.get("supermarket_store_type", {}).get("radio_percentage", 0),
# Hypermarket store type
"H39": user_data.get("hypermarket_store_type", {}).get("stores_number", 0),
"C39": user_data.get("hypermarket_store_type", {}).get("monthly_transactions", 0),
# Convert boolean to 1/0 for has_digital_screens
"I39": 1 if user_data.get("hypermarket_store_type", {}).get("has_digital_screens", False) else 0,
"J39": user_data.get("hypermarket_store_type", {}).get("screen_count", 0),
"K39": user_data.get("hypermarket_store_type", {}).get("screen_percentage", 0),
# Convert boolean to 1/0 for has_in_store_radio
"M39": 1 if user_data.get("hypermarket_store_type", {}).get("has_in_store_radio", False) else 0,
"N39": user_data.get("hypermarket_store_type", {}).get("radio_percentage", 0),
# Website, App, Loyalty
"B43": user_data.get("website_visitors", 0),
"B44": user_data.get("app_users", 0),
"B45": user_data.get("loyalty_users", 0),
# Social Media
"B49": user_data.get("facebook_followers", 0),
"B50": user_data.get("instagram_followers", 0),
"B51": user_data.get("google_views", 0)
}
# Inject values into the Variables sheet
print(f"Injecting variables into sheet: {variables_sheet.title}")
for cell_ref, value in cell_mappings.items():
try:
# Check if cell exists
if cell_ref in variables_sheet:
variables_sheet[cell_ref] = value
print(f"Set {cell_ref} = {value}")
else:
print(f"Warning: Cell {cell_ref} not found in sheet")
except Exception as e:
print(f"Warning: Could not set value for cell {cell_ref}: {e}")
# Save the workbook
workbook.save(excel_path)
print(f"Variables successfully injected into {excel_path}")
except Exception as e:
print(f"Error injecting variables: {e}")
if __name__ == "__main__":
create_excel_from_template()