PDF Reports Reference manual

Core functions

pdf_reports.pdf_reports.preload_stylesheet(path, is_scss='auto')[source]

Preload a stylesheet as a WeasyPrint CSS object once and for all.

Returns a weasyprint.CSS object which can be provided as-is in a list of default_stylesheets or extra_stylesheets.

Preloading stylesheets can save a lot of time for large CSS frameworks that are used several times. It prevents weasyprint from parsing the CSS every time.

If the path ends with .scss or .sass and is_scss is set to “auto”, is_scss will be set to True.

If is_scss is true, the file is compiled using python-libsass ( which must be installed).

Note: if you already have a string, directly use sass.compile(s) to compile the string.

pdf_reports.pdf_reports.pug_to_html(path=None, string=None, **context)[source]

Convert a Pug template, as file or string, to html.

Parameters:
  • path (str, optional) – Path to a .pug template file. The string parameter can be provided instead.

  • string (str, optional) – A string of a Pug template. The path parameter can be provided instead.

  • **context – Keyword arguments indicating the variables to use in the Pug template (if it contains variables). For instance title=’My title’.

pdf_reports.pdf_reports.write_report(html, target=None, base_url=None, use_default_styling=True, extra_stylesheets=())[source]

Write the provided HTML in a PDF file.

Parameters:
  • html (str) – A HTML string.

  • target (str or file-like object, optional) – A PDF file path or file-like object, or None for returning the raw bytes of the PDF.

  • base_url (str) – The base path from which relative paths in the HTML template start.

  • use_default_styling (bool) – Setting this parameter to False, your PDF will have no styling at all by default. This means no Semantic UI, which can speed up the rendering.

  • extra_stylesheets (list of str) – List of paths to other “.css” files used to define new styles or overwrite default styles.

Tools

Utilities for report generation.

The module contains in particular routines the creation of tables, plots, etc. inside the templates. Functions in this module are available from inside the templates under the domain name pdf_tools. For instance pdf_tools.dataframe_to_html().

class pdf_reports.tools.JupyterPDF(url, width=600, height=800)[source]

Class to display PDFs in a Jupyter / IPython notebook.

Just write this at the end of a code Cell to get in-browser PDF preview:

>>> from pdf_reports import JupyterPDF
>>> JupyterPDF("path_to_some.pdf")

Credits to StackOverflow’s Jakob: https://stackoverflow.com/a/19470377

pdf_reports.tools.add_css_class(element, cls)[source]

Add a given class to the given BeautifulSoup HTML element.

pdf_reports.tools.dataframe_to_html(dataframe, extra_classes=(), index=False, header=True, use_default_classes=True, escape_html=False)[source]

Return a HTML version of a dataframe with Semantic UI CSS style classes.

By default, it applies the following Semantic UI classes: ‘ui’, ‘compact’, ‘celled’, ‘striped’, ‘table’, ‘groups’.

Parameters:
  • dataframe (DataFrame) – The pandas dataframe to convert to HTML.

  • extra_classes (list of str) – Classes to add to the default, which are ‘ui’, ‘compact’, ‘celled’, ‘striped’, ‘table’, ‘groups’, selected to create nicely-formatted Semantic UI tables. For instance, ‘definition’ can be added to add special emphasis on the first column. See Semantic UI documentation.

  • index (bool) – Whether to display the dataframe’s index.

  • header (bool) – Whether to display the dataframe’s headers.

  • escape_html (bool) – Whether the content of the dataframe should be html-escaped. Leave to False if your dataframe contains images or any kind of HTML formatting.

pdf_reports.tools.figure_data(fig, size=None, fmt='png', bbox_inches='tight', **kwargs)[source]

Return a HTML-embeddable string of the figure data.

The string can be embedded in an image tag as <img src=”{DATA}”/>.

Parameters:
  • fig (Matplotlib figure or axis) – A Matplotlib figure. A Matplotlib “ax” can also be provided, at which case the whole ax.figure will be displayed (i.e., all axes in the same figure).

  • size (tuple) – Size or resolution (width, height) of the final figure image, in inches.

  • fmt (str) – Image format, for instance “png”, “svg”, “jpeg”. SVG is vectorial (non pixelated) but sometimes more difficult to work with inside HTML/PDF documents.

  • bbox_inches (str) – Keeping this option to “tight” will ensure that your plot’s delimitation is optimal.

  • **kwargs – Any other option of Matplotlib’s figure.savefig() method.

pdf_reports.tools.style_table_rows(table_html, tr_modifier)[source]

Return a new HTML string of the table, with rows modified.

Parameters:
  • table_html (str) – A string “<table>…</table>” of an HTML table.

  • tr_modifier (function) – A function that takes a BeautifulSoup tr element as argument and changes its attributes inplace. For instance, modifications can be made with tr.text = new_text, or with the add_css_class method.