Last modified: Nov 14, 2025 By Alexander Williams

Format Excel Tables with Python openpyxl

Excel tables transform raw data into structured formats. They offer automatic filtering and styling. Python openpyxl makes table creation easy.

This guide covers table formatting from basics to advanced features. You will learn practical techniques for data organization.

Why Use Excel Tables?

Tables provide many benefits over regular ranges. They automatically expand with new data. Filtering and sorting become effortless.

Tables maintain consistent formatting. They make data analysis more efficient. References stay accurate as data grows.

For related cell management techniques, see our guide on Excel Named Ranges Python openpyxl Guide.

Getting Started with openpyxl

First, ensure openpyxl is installed. Use pip for installation.


pip install openpyxl

Import the necessary modules. You will need Workbook and Table classes.


from openpyxl import Workbook
from openpyxl.worksheet.table import Table, TableStyleInfo

Creating Your First Table

Start by creating a workbook and adding sample data. Tables require structured data ranges.


# Create workbook and select active sheet
wb = Workbook()
ws = wb.active

# Add sample data
data = [
    ['Product', 'Category', 'Price', 'Quantity'],
    ['Laptop', 'Electronics', 999, 15],
    ['Desk', 'Furniture', 299, 8],
    ['Monitor', 'Electronics', 199, 12],
    ['Chair', 'Furniture', 149, 20]
]

# Write data to worksheet
for row in data:
    ws.append(row)

Defining the Table Range

Tables need a defined range. Calculate the range dynamically based on data size.


# Calculate table range dynamically
max_row = ws.max_row
max_col = ws.max_column

# Convert to Excel range notation (e.g., A1:D5)
table_range = f"A1:{chr(64 + max_col)}{max_row}"
print(f"Table range: {table_range}")

Table range: A1:D5

Creating the Table Object

Create a Table object with your calculated range. Set a descriptive name for reference.


# Create table object
table = Table(displayName="SalesData", ref=table_range)

# Define table style
style = TableStyleInfo(
    name="TableStyleMedium9",
    showFirstColumn=False,
    showLastColumn=False,
    showRowStripes=True,
    showColumnStripes=False
)

table.tableStyleInfo = style

Adding Table to Worksheet

Use the add_table method to apply your table. This creates the structured table format.


# Add table to worksheet
ws.add_table(table)

# Save the workbook
wb.save("formatted_table.xlsx")

Table Styling Options

openpyxl offers various built-in table styles. Choose from light, medium, and dark variants.

Common style families include TableStyleLight, TableStyleMedium, and TableStyleDark. Each has numbered variants.


# Available style examples
light_styles = ["TableStyleLight1", "TableStyleLight8"]
medium_styles = ["TableStyleMedium2", "TableStyleMedium9"]
dark_styles = ["TableStyleDark3", "TableStyleDark6"]

Working with Table Columns

Define custom column names and formulas. This enhances table functionality.


# Add calculated column for total value
ws['E1'] = 'Total Value'
for row in range(2, 6):  # Rows 2-5
    ws[f'E{row}'] = f'=C{row}*D{row}'

# Update table range to include new column
updated_range = f"A1:E{ws.max_row}"
table.ref = updated_range

For advanced formula handling, explore our Python openpyxl Formulas and Cell Evaluation guide.

Advanced Table Features

Tables support header row formatting and total rows. Configure these based on your needs.


# Enable total row with specific functions
table.tableStyleInfo.showRowStripes = True
# Total row would require additional configuration

Practical Example: Sales Report Table

Create a comprehensive sales report table. This demonstrates real-world application.


# Create sales report workbook
sales_wb = Workbook()
sales_ws = sales_wb.active

# Sales data with more columns
sales_data = [
    ['Date', 'Region', 'Salesperson', 'Product', 'Units', 'Revenue'],
    ['2024-01-15', 'North', 'Alice', 'Laptop', 5, 4995],
    ['2024-01-16', 'South', 'Bob', 'Monitor', 8, 1592],
    ['2024-01-17', 'East', 'Charlie', 'Chair', 12, 1788],
    ['2024-01-18', 'West', 'Diana', 'Desk', 3, 897]
]

# Write data and create table
for row in sales_data:
    sales_ws.append(row)

sales_table_range = f"A1:F{len(sales_data)}"
sales_table = Table(displayName="SalesReport", ref=sales_table_range)

# Apply professional styling
sales_style = TableStyleInfo(
    name="TableStyleMedium4",
    showFirstColumn=False,
    showLastColumn=False,
    showRowStripes=True,
    showColumnStripes=False
)

sales_table.tableStyleInfo = sales_style
sales_ws.add_table(sales_table)
sales_wb.save("sales_report_table.xlsx")

Combine tables with other features like Python openpyxl Conditional Formatting Guide for enhanced visualization.

Best Practices for Table Management

Use descriptive table names without spaces. This ensures compatibility and easy reference.

Always calculate ranges dynamically. This prevents errors when data changes.

Choose appropriate table styles. Consider readability and professional appearance.

Test tables with various data sizes. Ensure formatting scales properly.

Common Issues and Solutions

Table range errors often occur with incorrect coordinates. Double-check your range calculations.

Name conflicts can happen with multiple tables. Use unique, descriptive names for each table.

Style not applying? Verify style name spelling and table addition to worksheet.

Conclusion

Excel tables created with openpyxl provide powerful data organization. They automate formatting and improve data management.

You learned to create, style, and manage tables programmatically. These skills enhance your Excel automation capabilities.

Tables work well with other openpyxl features. They form the foundation for professional Excel reports.

Start implementing tables in your Python Excel projects today. Experience the benefits of structured data management.