Document
paper-docx 0.2.0 API reference
WordprocessingML (WML) document.
Not intended to be constructed directly. Use :func:docx.Document to open or create
a document.
Document(element: CT_Document, part: DocumentPart)Attributes
comments
attributecommentsCommentsA Comments object providing access to comments added to the document.
core_properties
attributecore_propertiesA CoreProperties object providing Dublin Core properties of document.
element
attributeelementThe lxml element proxied by this object.
inline_shapes
attributeinline_shapesThe InlineShapes collection for this document.
An inline shape is a graphical object, such as a picture, contained in a run of text and behaving like a character glyph, being flowed like other text in a paragraph.
paragraphs
attributeparagraphsList[Paragraph]The Paragraph instances in the document, in document order.
Note that paragraphs within revision marks such as <w:ins> or <w:del> do
not appear in this list.
part
attributepartDocumentPartThe DocumentPart object of this document.
revisions
attributerevisionsRevisions object enumerating tracked changes across all story parts.
paper-docx addition: a fresh snapshot on each access; supports
accept/reject of all revisions or filtered by author. See
docx.revision.
sections
attributesectionsSectionsSections object providing access to each section in this document.
settings
attributesettingsSettingsA Settings object providing access to the document-level settings.
styles
attributestylesA Styles object providing access to the styles in this document.
tables
attributetablesList[Table]All Table instances in the document, in document order.
Note that only tables appearing at the top level of the document appear in this
list; a table nested inside a table cell does not appear. A table within
revision marks such as <w:ins> or <w:del> will also not appear in the
list.
Functions
add_comment
funcadd_comment(runs: Run | Sequence[Run], text: str | None = '', author: str = '', initials: str | None = '') -> CommentAdd a comment anchored to runs, and return it.
Pass one Run or a sequence; only the first and last are used, so handing over
paragraph.runs works. Reach for Span.comment instead when the anchor should match
exact text rather than whole runs. Refuses a protected document, runs belonging to
another document, and a comments part that is missing or ambiguous.
paramrunsRun | Sequence[Run]paramtextstr | None= ''paramauthorstr= ''paraminitialsstr | None= ''Returns
docx.comments.Commentadd_heading
funcadd_heading(text: str = '', level: int = 1)Return a heading paragraph newly added to the end of the document.
The heading paragraph will contain text and have its paragraph style
determined by level. If level is 0, the style is set to Title. If level
is 1 (or omitted), Heading 1 is used. Otherwise the style is set to Heading {level}. Raises |ValueError| if level is outside the range 0-9.
paramtextstr= ''paramlevelint= 1add_page_break
funcadd_page_break()Return newly Paragraph object containing only a page break.
add_paragraph
funcadd_paragraph(text: str = '', style: str | ParagraphStyle | None = None) -> ParagraphReturn paragraph newly added to the end of the document.
The paragraph is populated with text and having paragraph style style.
text can contain tab (\t) characters, which are converted to the
appropriate XML form for a tab. text can also include newline (\n) or
carriage return (\r) characters, each of which is converted to a line
break.
paramtextstr= ''paramstylestr | ParagraphStyle | None= NoneReturns
docx.text.paragraph.Paragraphadd_picture
funcadd_picture(image_path_or_stream: str | IO[bytes], width: int | Length | None = None, height: int | Length | None = None)Return new picture shape added in its own paragraph at 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.
paramimage_path_or_streamstr | IO[bytes]paramwidthint | Length | None= Noneparamheightint | Length | None= Noneadd_section
funcadd_section(start_type: WD_SECTION = WD_SECTION.NEW_PAGE)Return a Section object newly added at the end of the document.
The optional start_type argument must be a member of the :ref:WdSectionStart
enumeration, and defaults to WD_SECTION.NEW_PAGE if not provided.
paramstart_typeWD_SECTION= WD_SECTION.NEW_PAGEadd_table
funcadd_table(rows: int, cols: int, style: str | _TableStyle | None = None)Add a table having row and column counts of rows and cols respectively.
style may be a table style object or a table style name. If style is None,
the table inherits the default table style of the document.
paramrowsintparamcolsintparamstylestr | _TableStyle | None= Noneiter_inner_content
funciter_inner_content() -> Iterator[Paragraph | Table]Generate each Paragraph or Table in this document in document order.
Returns
typing.Iterator[docx.text.paragraph.Paragraph | docx.table.Table]save
funcsave(path_or_stream: str | IO[bytes])Save this document to path_or_stream.
path_or_stream can be either a path to a filesystem location (a string) or a
file-like object.
parampath_or_streamstr | IO[bytes]