class DocxDocument(_GenericDocumentOutput): (source)
Constructor: DocxDocument(file_path, margins, autoupdate_fields, tags_allowed, ...)
Export a Report as MS Word docx file.
The docx (known as python-docx) package is used for that.
| Method | __init__ |
Ctor. |
| Method | add |
Add a user-definied paragraph style to the document. |
| Method | available |
Return a dict of available styles. |
| Method | control |
Convert _ControlType elements into MS Word document components. |
| Method | dataframe |
Convert a pandas.DataFrame in a MS Word table. |
| Method | image |
Embed an image into the document. |
| Method | page |
Set the page numbering behavior. |
| Method | save |
Start the export of each element into the docx file. |
| Method | set |
Page/section in landscape orientation. |
| Method | set |
Page/section in portrait orientation. |
| Method | string |
Add heading. |
| Method | string |
Add paragraph. |
| Method | strings |
Add a paragraph as enumeration item. |
| Method | toggle |
Toggle the current page/section's orientation. |
| Instance Variable | file |
Path to the file. |
| Static Method | _add |
Use in _add_row_and_column_labels(). |
| Static Method | _add |
Use in _add_row_and_column_labels(). |
| Static Method | _add |
Add a _field_ to an existing _run_. |
| Static Method | _add |
Add a page number field to an existing paragraph. |
| Static Method | _add |
Format the labels of rows and columns. |
| Static Method | _add |
Use in _add_row_and_column_labels(). |
| Static Method | _add |
Use in _add_row_and_column_labels(). |
| Static Method | _cell |
Convert pandas.DataFrame cell values to a MS Word table. |
| Static Method | _color |
Convert "color" value into docx (python-docx) format. |
| Static Method | _element |
Create an XML element with attribute and value. |
| Static Method | _multiindex |
Convert a MultiIndex to dictionary. |
| Static Method | _table |
Autofit objects in table. |
| Method | _activate |
All fields will semi-automatic updated. |
| Method | _caption |
Add caption paragraph including name and numbering. |
| Method | _init |
Initiate and setup the document instance self._doc. |
| Method | _init |
Set up the header and footer elements of the document. |
| Method | _init |
Create some additional styles. |
| Method | _set |
See __init__() for details. |
| Method | _set |
Set page orientation if different. |
| Method | _setup |
Documents language. |
| Method | _setup |
Undocumented |
| Method | _table |
Generic method to create a reference list (e.g. TOC). |
| Instance Variable | _additional |
List of user defined styles add via add_paragraph_style(). |
| Instance Variable | _autoupdate |
Activate the auto update field feature. |
| Instance Variable | _captions |
Position of figure captions on top (0) or bottom of a figure. Bottom is default. |
| Instance Variable | _captions |
Position of table captions on top (0) or bottom of a table. Bottom is default. |
| Instance Variable | _doc |
Instance of docx.document.Document. |
| Instance Variable | _footer |
String for the document footer. |
| Instance Variable | _header |
String for the document header. |
| Instance Variable | _language |
The language relevant for spell checking. |
| Instance Variable | _margins |
A 4 item tuple with millimeter values clockwise starting with top. |
| Instance Variable | _page |
A dict with settings about page numbering. |
| Instance Variable | _spell |
Document global checking of spelling and grammar. |
Inherited from _GenericDocumentOutput:
| Method | show |
Open the document with the associated application. |
| Instance Variable | tags |
List of tags for elements that are allowed. |
| Instance Variable | tags |
List of tags for elements that are excluded. |
| Method | _not |
Handle not implemented elements and throws a warning. |
pathlib.Path, *, margins: str | tuple = 'narrow', autoupdate_fields: bool = True, tags_allowed: list[ str] | str = None, tags_excluded: list[ str] | str = None, text_header: str = None, text_footer: str = None, spell_and_grammar_checking: bool = False, language: str = None, captions_position_table: int = 1, captions_position_figure: int = 1):
(source)
¶
Ctor.
| Parameters | |
filepathlib.Path | Path to the docx file to generate. |
margins:str | tuple | Names of predefined margins (narrow or moderate), a 4 item tuple with millimeter values clockwise starting with top, or a 2 item tuple with millimeter values for top-buttom and left-right. |
autoupdatebool | Activate auto-updated all fields in the docx. |
tagslist[ | See _GenericDocumentOutput. |
tagslist[ | See _GenericDocumentOutput. |
textstr | Text to put in document header. |
textstr | Text to put in document footer. |
spellbool | Activate checking of spelling and grammar. |
language:str | Specify language used. |
captionsint | Captions before (0) or after (1, default) a table. |
captionsint | Captions before (0) or after (1, default) a figure. |
str, *, based_on: str = None, font_name: str = None, font_pt: int = None, font_color: tuple[ int, int, int] | str = None, font_bgcolor: tuple[ int, int, int] | str = None):
(source)
¶
Add a user-definied paragraph style to the document.
A style can then be used via its name when adding new paragraphs:
doc = DocxDocument(fp) report = Report() # Create the style doc.add_paragraph_style(name='MyStyle', font_name='Fira Code') # Use the style report.paragraph('lore ipsum', style='MyStlye')
| Parameters | |
name:str | Name of the new style. |
basedstr | Parent style that style should inherit from. |
fontstr | Font name. |
fontint | Font size in points. |
fonttuple[ | Foreground color as name, Hex-value or RGB value tuple. |
fonttuple[ | Background color of the font. |
Convert _ControlType elements into MS Word document components.
Such components are page breaks, page orientation, reference lists for headings, figures and tables.
pandas.DataFrame, *, caption: str, note: str, autofit: bool, decimal_places: int = 2):
(source)
¶
Convert a pandas.DataFrame in a MS Word table.
Long tables can cause performance problems. A warning is logged on long tables.
| Parameters | |
df:pandas.DataFrame | The data frame. |
caption:str | String for the table caption. |
note:str | Used as (foot)note. |
autofit:bool | Auto adjust the width of the table to its content. |
decimalint | Round of float values. |
Embed an image into the document.
The original picture source is not scaled, resized or converted. Scaling is done "dynamic" via MS Word specifying the view-size in Millimeters. That value is calculated based on original pixel size, the DPI and the scaling factor scale_factor.
Plots and figures from matplotlib or derivates are saved in
PNG format with DEFAULT_DPI dpi.
| Parameters | |
imagebytes | The image as bytes. |
scalefloat | Factor to scale. |
caption:str | Image caption string. |
Start the export of each element into the docx file.
| Parameters | |
report:Report | The report instance with all elements. |
| Returns | |
_GenericDocumentOutput | Itself. |
Add a _field_ to an existing _run_.
A _run_ is a part of a paragraph. Fields are visible in MS Word as elements with gray background (e.g. for page numbers or citation references).
Credits: https://github.com/python-openxml/python-docx /issues/498#issuecomment-394143566
| Parameters | |
| run | The run. |
| field | The field. |
def _add_page_number(paragraph:
docx.text.paragraph.Paragraph, with_pagenum: bool, prefix: str, infix: str, suffix: str):
(source)
¶
Add a page number field to an existing paragraph.
Convert pandas.DataFrame cell values to a MS Word table.
The original cell_value is converted into a string. Decimal point
and Thousand delimiter used based on the current locale.LC_NUMERIC. A
float is rounded based on decimal_places using round(). Items of
an iterable are treated the same way.
def _color_to_rgbcolor(color:
str | tuple[ int, int, int]) -> docx.shared.RGBColor:
(source)
¶
def _element_with_attribute(element_name:
str, attribute_name: str, attribute_value: str) -> docx.oxml.OxmlElement:
(source)
¶
Create an XML element with attribute and value.
Convert a MultiIndex to dictionary.
Helper function used by _add_row_labels_two_levels() and
_add_column_labels_two_levels().
| Parameters | |
idx:pandas.MultiIndex | Index object (columns or rows of a pandas dataframe) |
| Returns | |
dict | The dictionary indexed by the first level of the multi index. |
Autofit objects in table.
Credits: https://github.com/python-openxml/python-docx /issues/209#issuecomment-566128709
All fields will semi-automatic updated.
This includes table of content and numbering in table and figure captions. In practice when opening such a docx file MS Word will ask if all fields should be updated.
Add caption paragraph including name and numbering.
Based on: https://github.com/python-openxml/python-docx/issues/359
| Parameters | |
caption:str | The content of the caption. |
target:str | The prefix name (e.g. _Table_ or _Figure_) |
Initiate and setup the document instance self._doc.
The method is not called by __init__() but save().
Set up the header and footer elements of the document.
The behavior is based on the instance attributes _page_numbering,
_header_text and _footer_text.
Generic method to create a reference list (e.g. TOC).
It is used for table_of_contents().
Credits
The code is based on the following sources - ttps://stackoverflow.com/a/59170642/4865723 - https://github.com/python-openxml/python-docx/issues/36 - https://github.com/xiaominzhaoparadigm/python-docx-add-list-of-tables-figures/blob/master/LOT.py