1+ # pyright: reportImportCycles=false
2+ # pyright: reportPrivateUsage=false
3+
14"""|Document| and closely related objects."""
25
6+ from __future__ import annotations
7+
8+ from typing import IO , TYPE_CHECKING , List
9+
310from docx .blkcntnr import BlockItemContainer
411from docx .enum .section import WD_SECTION
512from docx .enum .text import WD_BREAK
613from docx .section import Section , Sections
714from docx .shared import ElementProxy , Emu
8- from docx .text .paragraph import Paragraph
15+
16+ if TYPE_CHECKING :
17+ from docx import types as t
18+ from docx .oxml .document import CT_Body , CT_Document
19+ from docx .parts .document import DocumentPart
20+ from docx .settings import Settings
21+ from docx .shared import Length
22+ from docx .styles .style import ParagraphStyle , _TableStyle
23+ from docx .table import Table
24+ from docx .text .paragraph import Paragraph
925
1026
1127class Document (ElementProxy ):
@@ -15,12 +31,13 @@ class Document(ElementProxy):
1531 a document.
1632 """
1733
18- def __init__ (self , element , part ):
34+ def __init__ (self , element : CT_Document , part : DocumentPart ):
1935 super (Document , self ).__init__ (element )
36+ self ._element = element
2037 self ._part = part
2138 self .__body = None
2239
23- def add_heading (self , text = "" , level = 1 ):
40+ def add_heading (self , text : str = "" , level : int = 1 ):
2441 """Return a heading paragraph newly added to the end of the document.
2542
2643 The heading paragraph will contain `text` and have its paragraph style
@@ -39,7 +56,9 @@ def add_page_break(self):
3956 paragraph .add_run ().add_break (WD_BREAK .PAGE )
4057 return paragraph
4158
42- def add_paragraph (self , text : str = "" , style = None ) -> Paragraph :
59+ def add_paragraph (
60+ self , text : str = "" , style : str | ParagraphStyle | None = None
61+ ) -> Paragraph :
4362 """Return paragraph newly added to the end of the document.
4463
4564 The paragraph is populated with `text` and having paragraph style `style`.
@@ -51,7 +70,12 @@ def add_paragraph(self, text: str = "", style=None) -> Paragraph:
5170 """
5271 return self ._body .add_paragraph (text , style )
5372
54- def add_picture (self , image_path_or_stream , width = None , height = None ):
73+ def add_picture (
74+ self ,
75+ image_path_or_stream : str | IO [bytes ],
76+ width : int | Length | None = None ,
77+ height : int | Length | None = None ,
78+ ):
5579 """Return new picture shape added in its own paragraph at end of the document.
5680
5781 The picture contains the image at `image_path_or_stream`, scaled based on
@@ -65,7 +89,7 @@ def add_picture(self, image_path_or_stream, width=None, height=None):
6589 run = self .add_paragraph ().add_run ()
6690 return run .add_picture (image_path_or_stream , width , height )
6791
68- def add_section (self , start_type = WD_SECTION .NEW_PAGE ):
92+ def add_section (self , start_type : WD_SECTION = WD_SECTION .NEW_PAGE ):
6993 """Return a |Section| object newly added at the end of the document.
7094
7195 The optional `start_type` argument must be a member of the :ref:`WdSectionStart`
@@ -75,7 +99,7 @@ def add_section(self, start_type=WD_SECTION.NEW_PAGE):
7599 new_sectPr .start_type = start_type
76100 return Section (new_sectPr , self ._part )
77101
78- def add_table (self , rows , cols , style = None ):
102+ def add_table (self , rows : int , cols : int , style : str | _TableStyle | None = None ):
79103 """Add a table having row and column counts of `rows` and `cols` respectively.
80104
81105 `style` may be a table style object or a table style name. If `style` is |None|,
@@ -92,7 +116,7 @@ def core_properties(self):
92116
93117 @property
94118 def inline_shapes (self ):
95- """The |InlineShapes| collectoin for this document.
119+ """The |InlineShapes| collection for this document.
96120
97121 An inline shape is a graphical object, such as a picture, contained in a run of
98122 text and behaving like a character glyph, being flowed like other text in a
@@ -101,7 +125,7 @@ def inline_shapes(self):
101125 return self ._part .inline_shapes
102126
103127 @property
104- def paragraphs (self ):
128+ def paragraphs (self ) -> List [ Paragraph ] :
105129 """The |Paragraph| instances in the document, in document order.
106130
107131 Note that paragraphs within revision marks such as ``<w:ins>`` or ``<w:del>`` do
@@ -110,11 +134,11 @@ def paragraphs(self):
110134 return self ._body .paragraphs
111135
112136 @property
113- def part (self ):
137+ def part (self ) -> DocumentPart :
114138 """The |DocumentPart| object of this document."""
115139 return self ._part
116140
117- def save (self , path_or_stream ):
141+ def save (self , path_or_stream : str | IO [ bytes ] ):
118142 """Save this document to `path_or_stream`.
119143
120144 `path_or_stream` can be either a path to a filesystem location (a string) or a
@@ -123,12 +147,12 @@ def save(self, path_or_stream):
123147 self ._part .save (path_or_stream )
124148
125149 @property
126- def sections (self ):
150+ def sections (self ) -> Sections :
127151 """|Sections| object providing access to each section in this document."""
128152 return Sections (self ._element , self ._part )
129153
130154 @property
131- def settings (self ):
155+ def settings (self ) -> Settings :
132156 """A |Settings| object providing access to the document-level settings."""
133157 return self ._part .settings
134158
@@ -138,7 +162,7 @@ def styles(self):
138162 return self ._part .styles
139163
140164 @property
141- def tables (self ):
165+ def tables (self ) -> List [ Table ] :
142166 """All |Table| instances in the document, in document order.
143167
144168 Note that only tables appearing at the top level of the document appear in this
@@ -149,13 +173,13 @@ def tables(self):
149173 return self ._body .tables
150174
151175 @property
152- def _block_width (self ):
176+ def _block_width (self ) -> Length :
153177 """A |Length| object specifying the space between margins in last section."""
154178 section = self .sections [- 1 ]
155179 return Emu (section .page_width - section .left_margin - section .right_margin )
156180
157181 @property
158- def _body (self ):
182+ def _body (self ) -> _Body :
159183 """The |_Body| instance containing the content for this document."""
160184 if self .__body is None :
161185 self .__body = _Body (self ._element .body , self )
@@ -168,7 +192,7 @@ class _Body(BlockItemContainer):
168192 It's primary role is a container for document content.
169193 """
170194
171- def __init__ (self , body_elm , parent ):
195+ def __init__ (self , body_elm : CT_Body , parent : t . ProvidesStoryPart ):
172196 super (_Body , self ).__init__ (body_elm , parent )
173197 self ._body = body_elm
174198
0 commit comments