Hi All,
I need some help recreating some a script i have in Python in Alteryx,
I don't believe it can be done using the python node as contains packages not supported in Alteryx.
This is a simplified version of the problem but i believe if i can get this to work then i can build my solution around it
the aim is to
- Loads input data from an Excel file (InputDoc.xlsx) containing entities and associated values.
- Loads a template Excel file (Template.xlsx) which serves as the base for each tax pack.
- Creates a timestamped folder named Entity _Files [Date Time] in the same directory as the template to store the output files.
- Loops through each row in the input data:
- Inserts the entity name into cell A1 of the template.
- Populates values into cells C4 to C8.
- Adds a formula in C9 to sum the values.
- Unlocks cells D4 to D8 for editing.
- Protects the sheet with a password.
- Saves the customized file as [Entity].xlsx in the output folder.
- Prints a confirmation message with the location of the saved files.
Python code
import pandas as pd
import openpyxl
from openpyxl.styles import Protection
import os
from datetime import datetime
# Define file paths
data_file = r'C:\Users\Location\InputDoc.xlsx'
template_file = r'C:\Users\Location\Template.xlsx'
# Load the input data
data_df = pd.read_excel(data_file)
# Create a single timestamped output folder
timestamp = datetime.now().strftime('%Y-%m-%d %H-%M-%S')
base_dir = os.path.dirname(os.path.abspath(template_file))
output_dir = os.path.join(base_dir, f" Entity _Files {timestamp}")
os.makedirs(output_dir, exist_ok=True)
# Loop through each row in the input data
for _, row in data_df.iterrows():
entity = row['Entity']
if pd.isna(entity) or str(entity).strip() == '':
continue # Skip blank entities
# Load a fresh copy of the template
template_wb = openpyxl.load_workbook(template_file)
template_ws = template_wb.active
# Populate Entity in A1
template_ws['A1'].value = entity
# Populate values from the row
template_ws['C4'].value = row.get('Value1', 0)
template_ws['C5'].value = row.get('Value2', 0)
template_ws['C6'].value = row.get('Value3', 0)
template_ws['C7'].value = row.get('Value4', 0)
template_ws['C8'].value = row.get('Value5', 0)
# Set sum formula in C9
template_ws['C9'].value = '=SUM(C4:C8)'
# Unlock cells D4 to D8
for i in range(4, 9):
cell = template_ws[f'D{i}']
cell.protection = Protection(locked=False)
# Protect the sheet
template_ws.protection.password = 'LETMEIN'
template_ws.protection.sheet = True
# Save the new file in the single output folder
output_file = os.path.join(output_dir, f"{entity}.xlsx")
template_wb.save(output_file)
print(f" Processing complete. Files saved to:\n{output_dir}")Any questions let me know
Thanks Seb