pygauss.docs module

Created on Tue Jun 16 15:52:53 2015

@author: chris sewell

class pygauss.docs.MSDocument(docx=None)[source]

Bases: object

a class to output a Microsoft Word Document

inherited api details for docx.document.Document can be found at; https://python-docx.readthedocs.org/en/latest/api/document.html

the class has an internal state for the number of calls to add_picture and add_table for use in caption numbering

Parameters:docx (str or file-like object) – can be either a path to a .docx file (a string) or a file-like object. If docx is missing or None, the built-in default document “template” is loaded.
__dir__()[source]

required to have docx.document.Document methods in IPython tab completion

__getattr__(name)[source]

required to get docx.document.Document methods

add_dataframe(df, incl_indx=True, autofit=True, sig_figures=5, style='Medium List 1 Accent 1', caption=None)[source]

add dataframe as a table to the document

Parameters:
  • df (pandas.DataFrame) – a pandas dataframe
  • incl_indx (bool) – include dataframes index in table
  • autofit (bool) – allow table to autofit content
  • sig_figures (int) – number of significant figures for numbers in table
  • style (str) – MS Word table style
  • caption (str) – add a caption below the table
Returns:

pic – a table added to the document

Return type:

docx.table.Table

add_docstring(docstring, style='Body Text', markdown=True)[source]

adds a doctring to the document

this function will split text into paragraphs (denominated by a separating blank line) remove new-line characters and add to document, allowing for markdown style text designated in pygauss.docs.MSDocument.add_markdown()

Parameters:
  • text (str) – the text to add
  • style (str) – the style to apply for each paragraph
  • markdown (bool) – whether to apply markdown to the text
Returns:

paras – a list of paragraphs added to the document

Return type:

docx.text.paragraph.Paragraph

add_list(text_list=[], numbered=False)[source]

adds a list

add_markdown(text='', style='Body Text', markup_dict=None, para=None)[source]

adds a paragraph to the document, allowing for paragraph/font styling akin to a stripped down version of markdown text:

paragraph level:

# Header (level denoted by number of #'s)
- bullet list
1. numbered list

font level:

**bold**
*italic*
_{subscript}
^{superscript}
~~strikethrough~~
$mathML$
Parameters:
  • text (str) – the text to add
  • style (str) – the style to apply (overriden if paragraph level markdown)
  • markup_dict (dict) – if set will override built in font level markup {font_attribute:(start_chars, end_chars)}
  • para (docx.text.paragraph.Paragraph) – a pre-existing paragraph to add the text to if set, will ignore paragraph level markdown
Returns:

para – a paragraph added to the document

Return type:

docx.text.paragraph.Paragraph

add_mpl(fig, dpi=None, width=None, height=None, pad_inches=0.2, caption=None)[source]

add matplotlib figure to the document

Parameters:
  • fig (matplotlib.figure.Figure) – a matplotlib figure
  • dpi (int) – Dots per inch
  • width (float) – width of image in document
  • height (float) – width of image in document
  • pad_inches (float) – amount of padding around the figure
  • caption (str) – a caption below the figure
Returns:

pic – an inline picture added to the document

Return type:

docx.shape.InlineShape

add_picture(image_path_or_stream, width=None, height=None)[source]

Return a new picture shape added in its own paragraph at the end of the document. The picture contains the image at image_path_or_stream, scaled based on width and height. If neither width nor height is specified, the picture appears at its native size. If only one is specified, it is used to compute a scaling factor that is then applied to the unspecified dimension, preserving the aspect ratio of the image. The native size of the picture is calculated using the dots-per-inch (dpi) value specified in the image file, defaulting to 72 dpi if no value is specified, as is often the case.

add_table(rows, cols, style=None)[source]

Add a table having row and column counts of rows and cols respectively and table style of style. style may be a paragraph style object or a paragraph style name. If style is |None|, the table inherits the default table style of the document.