1
+ # pyright: reportImportCycles=false
2
+ # pyright: reportPrivateUsage=false
3
+
1
4
"""|Document| and closely related objects."""
2
5
6
+ from __future__ import annotations
7
+
8
+ from typing import IO , TYPE_CHECKING , List
9
+
3
10
from docx .blkcntnr import BlockItemContainer
4
11
from docx .enum .section import WD_SECTION
5
12
from docx .enum .text import WD_BREAK
6
13
from docx .section import Section , Sections
7
14
from 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
9
25
10
26
11
27
class Document (ElementProxy ):
@@ -15,12 +31,13 @@ class Document(ElementProxy):
15
31
a document.
16
32
"""
17
33
18
- def __init__ (self , element , part ):
34
+ def __init__ (self , element : CT_Document , part : DocumentPart ):
19
35
super (Document , self ).__init__ (element )
36
+ self ._element = element
20
37
self ._part = part
21
38
self .__body = None
22
39
23
- def add_heading (self , text = "" , level = 1 ):
40
+ def add_heading (self , text : str = "" , level : int = 1 ):
24
41
"""Return a heading paragraph newly added to the end of the document.
25
42
26
43
The heading paragraph will contain `text` and have its paragraph style
@@ -39,7 +56,9 @@ def add_page_break(self):
39
56
paragraph .add_run ().add_break (WD_BREAK .PAGE )
40
57
return paragraph
41
58
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 :
43
62
"""Return paragraph newly added to the end of the document.
44
63
45
64
The paragraph is populated with `text` and having paragraph style `style`.
@@ -51,7 +70,12 @@ def add_paragraph(self, text: str = "", style=None) -> Paragraph:
51
70
"""
52
71
return self ._body .add_paragraph (text , style )
53
72
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
+ ):
55
79
"""Return new picture shape added in its own paragraph at end of the document.
56
80
57
81
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):
65
89
run = self .add_paragraph ().add_run ()
66
90
return run .add_picture (image_path_or_stream , width , height )
67
91
68
- def add_section (self , start_type = WD_SECTION .NEW_PAGE ):
92
+ def add_section (self , start_type : WD_SECTION = WD_SECTION .NEW_PAGE ):
69
93
"""Return a |Section| object newly added at the end of the document.
70
94
71
95
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):
75
99
new_sectPr .start_type = start_type
76
100
return Section (new_sectPr , self ._part )
77
101
78
- def add_table (self , rows , cols , style = None ):
102
+ def add_table (self , rows : int , cols : int , style : str | _TableStyle | None = None ):
79
103
"""Add a table having row and column counts of `rows` and `cols` respectively.
80
104
81
105
`style` may be a table style object or a table style name. If `style` is |None|,
@@ -92,7 +116,7 @@ def core_properties(self):
92
116
93
117
@property
94
118
def inline_shapes (self ):
95
- """The |InlineShapes| collectoin for this document.
119
+ """The |InlineShapes| collection for this document.
96
120
97
121
An inline shape is a graphical object, such as a picture, contained in a run of
98
122
text and behaving like a character glyph, being flowed like other text in a
@@ -101,7 +125,7 @@ def inline_shapes(self):
101
125
return self ._part .inline_shapes
102
126
103
127
@property
104
- def paragraphs (self ):
128
+ def paragraphs (self ) -> List [ Paragraph ] :
105
129
"""The |Paragraph| instances in the document, in document order.
106
130
107
131
Note that paragraphs within revision marks such as ``<w:ins>`` or ``<w:del>`` do
@@ -110,11 +134,11 @@ def paragraphs(self):
110
134
return self ._body .paragraphs
111
135
112
136
@property
113
- def part (self ):
137
+ def part (self ) -> DocumentPart :
114
138
"""The |DocumentPart| object of this document."""
115
139
return self ._part
116
140
117
- def save (self , path_or_stream ):
141
+ def save (self , path_or_stream : str | IO [ bytes ] ):
118
142
"""Save this document to `path_or_stream`.
119
143
120
144
`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):
123
147
self ._part .save (path_or_stream )
124
148
125
149
@property
126
- def sections (self ):
150
+ def sections (self ) -> Sections :
127
151
"""|Sections| object providing access to each section in this document."""
128
152
return Sections (self ._element , self ._part )
129
153
130
154
@property
131
- def settings (self ):
155
+ def settings (self ) -> Settings :
132
156
"""A |Settings| object providing access to the document-level settings."""
133
157
return self ._part .settings
134
158
@@ -138,7 +162,7 @@ def styles(self):
138
162
return self ._part .styles
139
163
140
164
@property
141
- def tables (self ):
165
+ def tables (self ) -> List [ Table ] :
142
166
"""All |Table| instances in the document, in document order.
143
167
144
168
Note that only tables appearing at the top level of the document appear in this
@@ -149,13 +173,13 @@ def tables(self):
149
173
return self ._body .tables
150
174
151
175
@property
152
- def _block_width (self ):
176
+ def _block_width (self ) -> Length :
153
177
"""A |Length| object specifying the space between margins in last section."""
154
178
section = self .sections [- 1 ]
155
179
return Emu (section .page_width - section .left_margin - section .right_margin )
156
180
157
181
@property
158
- def _body (self ):
182
+ def _body (self ) -> _Body :
159
183
"""The |_Body| instance containing the content for this document."""
160
184
if self .__body is None :
161
185
self .__body = _Body (self ._element .body , self )
@@ -168,7 +192,7 @@ class _Body(BlockItemContainer):
168
192
It's primary role is a container for document content.
169
193
"""
170
194
171
- def __init__ (self , body_elm , parent ):
195
+ def __init__ (self , body_elm : CT_Body , parent : t . ProvidesStoryPart ):
172
196
super (_Body , self ).__init__ (body_elm , parent )
173
197
self ._body = body_elm
174
198