Python warning: no default style
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
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)
Solved! Go to Solution.
- Labels:
- Error Message
- Output
- Python
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
I tried running this in Alteryx 2021.4 and Alteryx 2023.1 and got no warning in either. In both, the Python version is 3.8.5 and openpyxl version is 3.1.2
You could try reinstalling openpyxl. In the Jupyter Notebook,
!pip uninstall openpyxl -y
!pip install openpyxl
(you may need to run as Alteryx as admin, or add a `--user` flag.
Alternatively, there looks to be another way to ignore Warnings here:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
I decided to take the 2nd approach, meaning to ignore the warning. The stackoverflow page gave me the general idea, and I used ChatGPT to provide the exact code I needed for it to work. Here it is (changes are in red):
from ayx import Alteryx
import os
from openpyxl import load_workbook
import warnings
# Define the directory path where your Excel files are located
directory_path = '\\\\servernamehere\\Alteryx\\Reconciliation'
# 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)
# Temporarily suppress the warning
with warnings.catch_warnings():
warnings.simplefilter("ignore")
# 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 ["Sheet1", "Sheet2", "Sheet3"]:
worksheet = workbook[sheet_name]
# Apply freeze panes
worksheet.freeze_panes = 'A2'
# Save the modified workbook
workbook.save(full_path)
