dataframe-image
dataframe-image is a Python library that enables embedding pandas DataFrames as images in PDF and Markdown files when converting from Jupyter Notebooks. It ensures DataFrames, including any applied styling, appear exactly as they do in a Jupyter Notebook, rather than as raw text or simple LaTeX tables. The library also provides functionality to export individual DataFrames as image files from Python scripts. It is actively maintained, with version 0.2.7 being the current release.
Common errors
-
No such file or directory: ...\AppData\Local\Temp\tmpXXXXXX\temp.png
cause The library failed to create or access temporary files/directories needed for image conversion, often due to permission issues, antivirus interference, or insufficient disk space.fixTry setting `table_conversion='matplotlib'` or `table_conversion='playwright'` to see if a different backend resolves the temporary file issue. Ensure your user has write permissions to the temporary directory. Restarting your environment or computer might also help clear transient issues. -
SyntaxError: not a PNG file
cause This error typically indicates that the underlying image generation process (usually by a browser backend) failed to produce a valid PNG file, often due to browser compatibility, missing browser, or driver issues.fixVerify that your selected browser (Chrome/Chromium for 'chrome'/'html2image', Firefox for 'selenium', or Playwright's browsers) is installed and accessible. Consider upgrading or downgrading your browser or `dataframe-image` package. Switching to `table_conversion='playwright'` (with `pip install "dataframe_image[playwright]"` and `playwright install`) is often a robust solution. -
WebDriverException: Message: 'chromedriver' executable needs to be in PATH.
cause The Chrome browser backend cannot find the necessary `chromedriver` executable, which is required for Selenium/Chrome automation. This can happen if Chrome is not installed or its driver is not in your system's PATH.fixEnsure Google Chrome is installed. If it is, `dataframe-image` often auto-detects it. If not, you might need to manually specify the path using the `chrome_path` parameter in `dfi.export()` or `dfi.convert()`, or install a browser using Playwright and use that backend. -
LaTeX command `xelatex` not found.
cause When converting Jupyter Notebooks to PDF using the default LaTeX option, the required LaTeX distribution (e.g., TeX Live or MiKTeX) is not installed on the system or not found in the PATH.fixInstall a full LaTeX distribution (e.g., TeX Live on Linux/macOS, MiKTeX on Windows). Alternatively, use a browser-based conversion method for PDF output by setting `to='pdf'` and configuring `table_conversion` to a browser backend (e.g., `'playwright'`).
Warnings
- breaking The default browser backend for image conversion changed to Playwright in v0.2.7. If you were relying on the previous Chrome-based default without explicit configuration, you might need to install 'dataframe_image[playwright]' or explicitly set 'table_conversion' to 'chrome' (if Chrome is installed) or 'selenium' (for Firefox).
- breaking Version 0.2.4 was the last to use `setup.py` for installation and allowed direct file insertion into Jupyter directories. Subsequent versions (using `pyproject.toml`) changed this integration, potentially affecting older Jupyter notebook workflows that relied on direct file placements for custom extensions.
- gotcha Browser-based image conversion (Chrome, Playwright, Selenium) requires the respective browser (Chromium-based or Firefox) to be installed on the system. Without it, conversion will fail. Browser updates can also introduce compatibility issues (e.g., Chrome v111, v129).
- gotcha The `matplotlib` backend, while faster and not requiring a browser, has limitations on styling. It can only simulate default header and cell styles (e.g., `background_gradient`, `font-size`) and does not fully support advanced CSS properties or features like `set_caption`.
- gotcha When running `dataframe_image` functions like `dfi.export` or `dfi.convert` within an asynchronous context, such as a Jupyter Notebook cell that is not the primary execution thread, you must use their asynchronous counterparts (e.g., `await dfi.export_async(...)`).
Install
-
pip install dataframe-image -
pip install "dataframe_image[playwright]"
Imports
- dataframe_image
import dataframe_image as dfi
- export
dataframe_image.export(...)
dfi.export(...)
Quickstart
import pandas as pd
import numpy as np
import dataframe_image as dfi
# Create a sample DataFrame
df = pd.DataFrame(np.random.randn(6, 4), columns=list('ABCD'))
# Apply some styling
df_styled = df.style.background_gradient(cmap='Blues').set_caption('My Styled DataFrame')
# Export the styled DataFrame as a PNG image
dfi.export(df_styled, 'styled_dataframe.png')
print("Styled DataFrame exported to styled_dataframe.png")