forked from python-openxml/python-docx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.py
More file actions
186 lines (164 loc) · 6.87 KB
/
api.py
File metadata and controls
186 lines (164 loc) · 6.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# encoding: utf-8
"""
Directly exposed API functions and classes, :func:`Document` for now.
Provides a syntactically more convenient API for interacting with the
OpcPackage graph.
"""
from __future__ import absolute_import, division, print_function
import os
from docx.enum.text import WD_BREAK
from docx.opc.constants import CONTENT_TYPE as CT, RELATIONSHIP_TYPE as RT
from docx.package import Package
from docx.parts.numbering import NumberingPart
from docx.parts.styles import StylesPart
from docx.shared import lazyproperty
_thisdir = os.path.split(__file__)[0]
_default_docx_path = os.path.join(_thisdir, 'templates', 'default.docx')
class Document(object):
"""
Return a |Document| instance loaded from *docx*, where *docx* can be
either a path to a ``.docx`` file (a string) or a file-like object. If
*docx* is missing or ``None``, the built-in default document "template"
is loaded.
"""
def __init__(self, docx=None):
super(Document, self).__init__()
document_part, package = self._open(docx)
self._document_part = document_part
self._package = package
def add_heading(self, text='', level=1):
"""
Return a heading paragraph newly added to the end of the document,
populated with *text* and having the heading paragraph style
determined by *level*. If *level* is 0, the style is set to
``'Title'``. If *level* is 1 (or not present), ``'Heading1'`` is used.
Otherwise the style is set to ``'Heading{level}'``. If *level* is
outside the range 0-9, |ValueError| is raised.
"""
if not 0 <= level <= 9:
raise ValueError("level must be in range 0-9, got %d" % level)
style = 'Title' if level == 0 else 'Heading%d' % level
return self.add_paragraph(text, style)
def add_page_break(self):
"""
Return a paragraph newly added to the end of the document and
containing only a page break.
"""
p = self._document_part.add_paragraph()
r = p.add_run()
r.add_break(WD_BREAK.PAGE)
return p
def add_paragraph(self, text='', style=None):
"""
Return a paragraph newly added to the end of the document, populated
with *text* and having paragraph style *style*.
"""
p = self._document_part.add_paragraph()
if text:
r = p.add_run()
r.add_text(text)
if style is not None:
p.style = style
return p
def add_picture(self, image_path_or_stream, width=None, height=None):
"""
Add the image at *image_path_or_stream* in a new paragraph at the end
of the document. 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.
"""
picture = self.inline_shapes.add_picture(image_path_or_stream)
# scale picture dimensions if width and/or height provided
if width is not None or height is not None:
native_width, native_height = picture.width, picture.height
if width is None:
scaling_factor = float(height) / float(native_height)
width = int(round(native_width * scaling_factor))
elif height is None:
scaling_factor = float(width) / float(native_width)
height = int(round(native_height * scaling_factor))
# set picture to scaled dimensions
picture.width = width
picture.height = height
return picture
def add_table(self, rows, cols, style='LightShading-Accent1'):
"""
Add a table having row and column counts of *rows* and *cols*
respectively and table style of *style*. If *style* is |None|, a
table with no style is produced.
"""
table = self._document_part.add_table(rows, cols)
if style:
table.style = style
return table
@property
def inline_shapes(self):
"""
Return a reference to the |InlineShapes| instance for this document.
"""
return self._document_part.inline_shapes
@lazyproperty
def numbering_part(self):
"""
Instance of |NumberingPart| for this document. Creates an empty
numbering part if one is not present.
"""
try:
return self._document_part.part_related_by(RT.NUMBERING)
except KeyError:
numbering_part = NumberingPart.new()
self._document_part.relate_to(numbering_part, RT.NUMBERING)
return numbering_part
@property
def paragraphs(self):
"""
A list of |Paragraph| instances corresponding to the paragraphs 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.
"""
return self._document_part.paragraphs
def save(self, path_or_stream):
"""
Save this document to *path_or_stream*, which can be either a path to
a filesystem location (a string) or a file-like object.
"""
self._package.save(path_or_stream)
@lazyproperty
def styles_part(self):
"""
Instance of |StylesPart| for this document. Creates an empty styles
part if one is not present.
"""
try:
return self._document_part.part_related_by(RT.STYLES)
except KeyError:
styles_part = StylesPart.new()
self._document_part.relate_to(styles_part, RT.STYLES)
return styles_part
@property
def tables(self):
"""
A list of |Table| instances corresponding to the tables in the
document, in document order. Note that tables within revision marks
such as ``<w:ins>`` or ``<w:del>`` do not appear in this list.
"""
return self._document_part.tables
@staticmethod
def _open(docx):
"""
Return a (document_part, package) 2-tuple loaded from *docx*, where
*docx* can be either a path to a ``.docx`` file (a string) or a
file-like object. If *docx* is ``None``, the built-in default
document "template" is loaded.
"""
docx = _default_docx_path if docx is None else docx
package = Package.open(docx)
document_part = package.main_document
if document_part.content_type != CT.WML_DOCUMENT_MAIN:
tmpl = "file '%s' is not a Word file, content type is '%s'"
raise ValueError(tmpl % (docx, document_part.content_type))
return document_part, package