pyladoc.DocumentWriter#

class pyladoc.DocumentWriter#

Bases: object

A class to create a document for exporting to HTML or LaTeX.

Initializes the DocumentWriter instance.

add_diagram(fig, caption='', ref_id='', prefix_pattern='Figure {}: ', ref_type='fig', centered=True)#

Adds a diagram to the document.

Parameters:
  • fig (None) – The figure to add (matplotlib figure)

  • caption (str) – The caption for the figure

  • ref_id (str) – If provided, the figure can be referenced by this string

  • prefix_pattern (str) – A custom string for the caption prefix, {} will be replaced by the figure number

  • ref_type (str) – The type of reference. Each type (e.g., ‘fig’, ‘table’) has an individual numbering

  • centered (bool) – Whether to center the figure in LaTeX output

Return type:

None

add_document(doc)#
Return type:

None

add_equation(latex_equation, ref_id='', ref_type='eq')#

Adds a LaTeX equation to the document.

Parameters:
  • latex_equation (str) – LaTeX formatted equation

  • ref_id (str) – If provided, the equation is displayed with a number and can be referenced by the ref_id

Return type:

None

add_h1(text)#

Adds a h1 heading to the document.

Parameters:

text (str) – The text of the heading

Return type:

None

add_h2(text)#

Adds a h2 heading to the document.

Parameters:

text (str) – The text of the heading

Return type:

None

add_h3(text)#

Adds a h3 heading to the document.

Parameters:

text (str) – The text of the heading

Return type:

None

add_html(text)#

Adds HTML formatted text to the document. For the LaTeX export only basic HTML for text formatting and tables is supported.

Parameters:

text (str) – The HTML to add to the document

Return type:

None

add_markdown(text, section_class='')#

Adds a markdown formatted text to the document.

Parameters:
  • text (str) – The markdown text to add

  • section_class (str) – The class for the text section

Return type:

None

add_table(table, caption='', ref_id='', prefix_pattern='Table {}: ', ref_type='table', centered=True)#

Adds a table to the document.

Parameters:
  • table (None) – The table to add (pandas DataFrame or Styler)

  • caption (str) – The caption for the table

  • ref_id (str) – If provided, the table can be referenced by this string

  • prefix_pattern (str) – A custom string for the caption prefix, {} will be replaced by the table number

  • ref_type (str) – The type of reference. Each type (e.g., ‘fig’, ‘table’) has an individual numbering

  • centered (bool) – Whether to center the table in LaTeX output

Return type:

None

add_text(text, section_class='')#

Adds a text paragraph to the document.

Parameters:
  • text (str) – The text to add

  • section_class (str) – The class for the paragraph

Return type:

None

new_field(name)#
Return type:

DocumentWriter

to_html(figure_format='svg', base64_svgs=False, figure_scale=1)#

Export the document to HTML. Figures will bew embedded in the HTML code. The format can be selected between png in base64, inline svg or svg in base64.

Parameters:
  • figure_format (Literal['svg', 'png', 'pgf']) – The format for embedding the figures in the HTML code (svg or png)

  • base64_svgs (bool) – Whether to encode svg images in base64

Return type:

str

Returns:

The HTML code

to_latex(font_family=None, table_renderer='simple', figure_scale=1)#

Export the document to LaTeX. Figures will be embedded as pgf graphics.

Parameters:
  • font_family (Literal[None, 'serif', 'sans-serif']) – Overwrites the front family for figures

  • table_renderer (Literal['pandas', 'simple']) – The renderer for tables (simple: renderer with column type guessing for text and numbers; pandas: using the internal pandas LaTeX renderer)

Return type:

str

Returns:

The LaTeX code

to_pdf(file_path, font_family=None, table_renderer='simple', latex_template_path='', fields_dict={}, figure_scale=1, engine='pdflatex')#

Export the document to a PDF file using LaTeX.

Parameters:
  • file_path (str) – The path to save the PDF file to

  • font_family (Literal[None, 'serif', 'sans-serif']) – Overwrites the front family for figures and the template

  • table_renderer (Literal['pandas', 'simple']) – The renderer for tables (simple: renderer with column type guessing for text and numbers; pandas: using the internal pandas LaTeX renderer)

  • latex_template_path (str) – Path to a LaTeX template file. The expression <!–CONTENT–> will be replaced by the generated content. If no path is provided a default template is used.

  • fields_dict (dict[str, str]) – A dictionary with field names as keys and content as values replacing the placeholders <!–KEY–> in the template.

  • figure_scale (float) – Scaling factor for the figure size

  • engine (Literal['pdflatex', 'lualatex', 'xelatex', 'tectonic']) – LaTeX engine (pdflatex, lualatex, xelatex or tectonic)

Return type:

bool

Returns:

True if the PDF file was successfully created