My workflow renders 1 output file and then applies formatting (e.g. freeze panes) to that output file via a Python tool. However, I get a warning that says "Workbook contains no default style, apply openpyxl's default." There is no actual problem here; I just want the warning to go away. I have tried actually applying formatting to the output file as well as using the openpyxl.utils.cell module to define a get_column_letter function as a workaround to make the error go away; nothing has worked so far. My code is as follows:
from ayx import Alteryx
import os
from openpyxl import load_workbook
from openpyxl.styles import NamedStyle, Font, Alignment
# Define the directory path where your Excel files are located
directory_path = '\\\\servernamehere\\Alteryx\\07-Reconciliation'
# Create a custom style
custom_style = NamedStyle(name="custom_style")
custom_style.font = Font(name='Calibri', size=11)
custom_style.alignment = Alignment(horizontal='left', vertical='center', wrap_text=True)
# Loop over files in the directory
for filename in os.listdir(directory_path):
if 'Output-Fuel Tax Recon' in filename and filename.endswith('.xlsx'):
# Construct the full file path
full_path = os.path.join(directory_path, filename)
# Load the Excel workbook
workbook = load_workbook(full_path)
# Loop over all sheets in the workbook
for sheet_name in workbook.sheetnames:
# Handle sheet names with spaces or special characters using quotes
if sheet_name in ["1-Cust Summ", "2-Detail", "3-Other"]:
worksheet = workbook[sheet_name]
# Apply the custom style to all cells in the worksheet
for row in worksheet.iter_rows():
for cell in row:
cell.style = custom_style
# Save the modified workbook
workbook.save(full_path)