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__ |
No summary |
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 their MS Word document corresponding 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 |
Current page/section in landscape orientation. |
Method | set |
Current page/section in portrait orientation. |
Method | string |
Add heading. |
Method | string |
Add paragraph. |
Method | toggle |
Toggle the current page/section's orientation. |
Static Method | _add |
Used by _add_row_and_column_labels() . |
Static Method | _add |
Used by _add_row_and_column_labels() . |
Static Method | _add |
Helper adding 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 |
Used by _add_row_and_column_labels() . |
Static Method | _add |
Used by _add_row_and_column_labels() . |
Static Method | _cell |
Helper for converting pandas.DataFrame cell values to a MS Word table. |
Static Method | _color |
Convert a "color" value into format usable by docx (aka python-docx). |
Static Method | _element |
Helper to create an XML element with an attribute and its value. |
Static Method | _multiindex |
Convert a MultiIndex to dictionary. |
Static Method | _table |
Helper to autofit table objects. |
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 |
Setup header and footer elements of the document. |
Method | _init |
Create some additional styles. |
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 string indicate the type of margins (narrow, moderate). |
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 generated document with the file type associated application. |
Instance Variable | file |
Path to the file. |
Instance Variable | tags |
List of tags for elements that are allowed. |
Instance Variable | tags |
List of tags for elements that are excluded. |
Method | _not |
Handler for not implemented elements. |
pathlib.Path
, margins: str
= 'narrow', autoupdate_fields: bool
= True, tags_allowed: Union[ List[ str], str]
= None, tags_excluded: Union[ 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)
¶
Parameters | |
filepathlib.Path | Path to the docx file to generate. |
margins:str | Possible values are narrow or moderate. |
autoupdatebool | Activate auto-updated all fields in the docx. |
tagsUnion[ | See _GenericDocumentOutput . |
tagsUnion[ | See _GenericDocumentOutput . |
textstr | Text to put in document header. |
textstr | Text to put in document footer. |
spellbool | Undocumented |
language:str | Undocumented |
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: Union[ tuple[ int, int, int], str]
= None, font_bgcolor: Union[ 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. |
fontUnion[ | Foreground color as name, Hex-value or RGB value tuple. |
fontUnion[ | Background color of the font. |
Convert _ControlType
elements into their MS Word document
corresponding 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.
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. |
Helper adding 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
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.
Helper for converting 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()
.
def _color_to_rgbcolor(color:
Union[ str, tuple[ int, int, int]]
) -> docx.shared.RGBColor
:
(source)
¶
Convert a "color" value into format usable by docx (aka python-docx).
The package webcolors
is used to do the conversion.
Parameters | |
color:Union[ | Name of a color, Hex Color code as string or an integer tuple with RGB values for red, green and blue. |
Returns | |
docx.shared.RGBColor | Undocumented |
def _element_with_attribute(element_name:
str
, attribute_name: str
, attribute_value: str
) -> docx.oxml.OxmlElement
:
(source)
¶
Helper to create an XML element with an attribute and its 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. |
Helper to autofit table objects.
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()
.
Setup 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