Pretty HTML Table for Pandas DataFrames
pretty-html-table (v0.9.16) is a Python library designed to convert pandas DataFrames into aesthetically pleasing HTML tables, primarily for use in email reports. It offers 12 different color themes and formats HTML at the DataFrame row level to ensure broad compatibility across various email providers, avoiding common CSS rendering issues. The library is currently in maintenance mode with infrequent releases, with the last significant update in April 2022.
Warnings
- gotcha When embedding the generated HTML into emails, ensure you use the `HTMLBody` property (e.g., `mail.HTMLBody` for Outlook objects) rather than `Body` to correctly render the table. Using `Body` will typically display the raw HTML text.
- gotcha The library prioritizes compatibility with email clients by applying styles at the HTML table row level, rather than relying heavily on external CSS. This approach ensures consistent rendering across many email services but means advanced CSS customizations or responsive design patterns typically used for web pages might not be directly supported or effective.
- gotcha When using the `width_dict` argument to set custom column widths, the provided list of width strings (e.g., `['100px', 'auto', '300px']`) must have a length exactly matching the number of columns in your pandas DataFrame. An incorrect length will lead to unexpected or unapplied width styles.
- gotcha The library appears to be in maintenance mode. The last PyPI release (v0.9.16) was in April 2022, and the GitHub repository shows the last significant commit approximately four years prior. While functional, active development for new features or rapid bug fixes may be limited.
Install
-
pip install pretty-html-table
Imports
- build_table
from pretty_html_table import build_table
Quickstart
import pandas as pd
from pretty_html_table import build_table
# Create a sample pandas DataFrame
data = {
'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [24, 27, 22],
'City': ['New York', 'Los Angeles', 'Chicago']
}
df = pd.DataFrame(data)
# Convert the DataFrame to a pretty HTML table with a 'blue_light' theme
html_table = build_table(df, 'blue_light', width='auto', font_size='medium', text_align='left')
# Print the HTML table (for demonstration)
print(html_table)
# Optionally, save to an HTML file
# with open('pretty_table.html', 'w') as f:
# f.write(html_table)
# print("HTML table saved to pretty_table.html")