python-pptx2 2.13.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pptx2/__init__.py +152 -0
- pptx2/_color.py +75 -0
- pptx2/_slide_importer.py +597 -0
- pptx2/_svg.py +155 -0
- pptx2/_template_applier.py +292 -0
- pptx2/_textstyle.py +187 -0
- pptx2/accessibility.py +365 -0
- pptx2/action.py +270 -0
- pptx2/animation.py +2237 -0
- pptx2/api.py +49 -0
- pptx2/audit.py +258 -0
- pptx2/chart/__init__.py +0 -0
- pptx2/chart/analytics.py +381 -0
- pptx2/chart/axis.py +543 -0
- pptx2/chart/category.py +200 -0
- pptx2/chart/chart.py +670 -0
- pptx2/chart/data.py +864 -0
- pptx2/chart/datalabel.py +406 -0
- pptx2/chart/legend.py +86 -0
- pptx2/chart/marker.py +70 -0
- pptx2/chart/palettes.py +129 -0
- pptx2/chart/plot.py +462 -0
- pptx2/chart/point.py +101 -0
- pptx2/chart/quick_layouts.py +325 -0
- pptx2/chart/series.py +334 -0
- pptx2/chart/xlsx.py +272 -0
- pptx2/chart/xmlwriter.py +1845 -0
- pptx2/compose/__init__.py +28 -0
- pptx2/compose/from_spec.py +1094 -0
- pptx2/design/__init__.py +8 -0
- pptx2/design/components.py +607 -0
- pptx2/design/figures.py +389 -0
- pptx2/design/layout.py +370 -0
- pptx2/design/recipes.py +1967 -0
- pptx2/design/style.py +209 -0
- pptx2/design/tokens.py +915 -0
- pptx2/diagrams.py +754 -0
- pptx2/dml/__init__.py +0 -0
- pptx2/dml/chtfmt.py +40 -0
- pptx2/dml/color.py +496 -0
- pptx2/dml/effect.py +909 -0
- pptx2/dml/fill.py +691 -0
- pptx2/dml/line.py +287 -0
- pptx2/dml/picture.py +212 -0
- pptx2/dml/three_d.py +381 -0
- pptx2/enum/__init__.py +0 -0
- pptx2/enum/action.py +71 -0
- pptx2/enum/animation.py +31 -0
- pptx2/enum/base.py +218 -0
- pptx2/enum/chart.py +574 -0
- pptx2/enum/dml.py +740 -0
- pptx2/enum/lang.py +685 -0
- pptx2/enum/presentation.py +133 -0
- pptx2/enum/shapes.py +1029 -0
- pptx2/enum/text.py +230 -0
- pptx2/exc.py +42 -0
- pptx2/formats.py +139 -0
- pptx2/geometry.py +420 -0
- pptx2/inherit.py +109 -0
- pptx2/lint.py +2256 -0
- pptx2/math.py +177 -0
- pptx2/media.py +197 -0
- pptx2/opc/__init__.py +0 -0
- pptx2/opc/constants.py +332 -0
- pptx2/opc/oxml.py +188 -0
- pptx2/opc/package.py +762 -0
- pptx2/opc/packuri.py +109 -0
- pptx2/opc/serialized.py +296 -0
- pptx2/opc/shared.py +20 -0
- pptx2/opc/spec.py +45 -0
- pptx2/oxml/__init__.py +555 -0
- pptx2/oxml/action.py +53 -0
- pptx2/oxml/chart/__init__.py +0 -0
- pptx2/oxml/chart/axis.py +337 -0
- pptx2/oxml/chart/chart.py +481 -0
- pptx2/oxml/chart/datalabel.py +253 -0
- pptx2/oxml/chart/legend.py +72 -0
- pptx2/oxml/chart/marker.py +61 -0
- pptx2/oxml/chart/plot.py +365 -0
- pptx2/oxml/chart/series.py +425 -0
- pptx2/oxml/chart/shared.py +220 -0
- pptx2/oxml/coreprops.py +288 -0
- pptx2/oxml/dml/__init__.py +0 -0
- pptx2/oxml/dml/color.py +135 -0
- pptx2/oxml/dml/effect.py +213 -0
- pptx2/oxml/dml/fill.py +316 -0
- pptx2/oxml/dml/line.py +12 -0
- pptx2/oxml/dml/three_d.py +110 -0
- pptx2/oxml/ns.py +135 -0
- pptx2/oxml/presentation.py +313 -0
- pptx2/oxml/shapes/__init__.py +19 -0
- pptx2/oxml/shapes/autoshape.py +467 -0
- pptx2/oxml/shapes/connector.py +107 -0
- pptx2/oxml/shapes/graphfrm.py +347 -0
- pptx2/oxml/shapes/groupshape.py +329 -0
- pptx2/oxml/shapes/picture.py +270 -0
- pptx2/oxml/shapes/shared.py +577 -0
- pptx2/oxml/simpletypes.py +1027 -0
- pptx2/oxml/slide.py +563 -0
- pptx2/oxml/table.py +650 -0
- pptx2/oxml/text.py +815 -0
- pptx2/oxml/theme.py +36 -0
- pptx2/oxml/xmlchemy.py +717 -0
- pptx2/package.py +222 -0
- pptx2/parts/__init__.py +0 -0
- pptx2/parts/chart.py +95 -0
- pptx2/parts/coreprops.py +167 -0
- pptx2/parts/diagram.py +37 -0
- pptx2/parts/embeddedpackage.py +93 -0
- pptx2/parts/image.py +275 -0
- pptx2/parts/media.py +37 -0
- pptx2/parts/presentation.py +136 -0
- pptx2/parts/slide.py +371 -0
- pptx2/presentation.py +408 -0
- pptx2/py.typed +0 -0
- pptx2/render.py +586 -0
- pptx2/section.py +272 -0
- pptx2/shapes/__init__.py +26 -0
- pptx2/shapes/autoshape.py +442 -0
- pptx2/shapes/base.py +1078 -0
- pptx2/shapes/connector.py +297 -0
- pptx2/shapes/freeform.py +337 -0
- pptx2/shapes/graphfrm.py +316 -0
- pptx2/shapes/group.py +264 -0
- pptx2/shapes/picture.py +422 -0
- pptx2/shapes/placeholder.py +468 -0
- pptx2/shapes/shapetree.py +2027 -0
- pptx2/shared.py +82 -0
- pptx2/skill/SKILL.md +450 -0
- pptx2/skill/__init__.py +78 -0
- pptx2/skill/__main__.py +64 -0
- pptx2/skill/references/animations.md +189 -0
- pptx2/skill/references/basics.md +421 -0
- pptx2/skill/references/charts.md +254 -0
- pptx2/skill/references/compose.md +234 -0
- pptx2/skill/references/design.md +366 -0
- pptx2/skill/references/effects.md +249 -0
- pptx2/skill/references/end-to-end-deck.md +231 -0
- pptx2/skill/references/geometry-and-arrows.md +334 -0
- pptx2/skill/references/lint.md +275 -0
- pptx2/skill/references/math.md +86 -0
- pptx2/skill/references/picture-effects.md +129 -0
- pptx2/skill/references/render.md +151 -0
- pptx2/skill/references/smart-art.md +75 -0
- pptx2/skill/references/space-aware-authoring.md +249 -0
- pptx2/skill/references/tables.md +244 -0
- pptx2/skill/references/theme.md +127 -0
- pptx2/skill/references/three-d.md +109 -0
- pptx2/skill/references/transitions.md +100 -0
- pptx2/slide.py +1244 -0
- pptx2/smart_art.py +220 -0
- pptx2/spec.py +633 -0
- pptx2/table.py +1181 -0
- pptx2/table_styles.py +184 -0
- pptx2/templates/default.pptx +0 -0
- pptx2/templates/docx-icon.emf +0 -0
- pptx2/templates/generic-icon.emf +0 -0
- pptx2/templates/notes.xml +23 -0
- pptx2/templates/notesMaster.xml +352 -0
- pptx2/templates/pptx-icon.emf +0 -0
- pptx2/templates/theme.xml +321 -0
- pptx2/templates/xlsx-icon.emf +0 -0
- pptx2/text/__init__.py +0 -0
- pptx2/text/fonts.py +482 -0
- pptx2/text/layout.py +374 -0
- pptx2/text/text.py +1272 -0
- pptx2/theme.py +721 -0
- pptx2/types.py +36 -0
- pptx2/util.py +263 -0
- python_pptx2-2.13.0.dist-info/METADATA +351 -0
- python_pptx2-2.13.0.dist-info/RECORD +175 -0
- python_pptx2-2.13.0.dist-info/WHEEL +5 -0
- python_pptx2-2.13.0.dist-info/entry_points.txt +3 -0
- python_pptx2-2.13.0.dist-info/licenses/LICENSE +22 -0
- python_pptx2-2.13.0.dist-info/top_level.txt +1 -0
pptx2/enum/text.py
ADDED
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
"""Enumerations used by text and related objects."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from pptx2.enum.base import BaseEnum, BaseXmlEnum
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class MSO_AUTO_SIZE(BaseEnum):
|
|
9
|
+
"""Determines the type of automatic sizing allowed.
|
|
10
|
+
|
|
11
|
+
The following names can be used to specify the automatic sizing behavior used to fit a shape's
|
|
12
|
+
text within the shape bounding box, for example::
|
|
13
|
+
|
|
14
|
+
from pptx2.enum.text import MSO_AUTO_SIZE
|
|
15
|
+
|
|
16
|
+
shape.text_frame.auto_size = MSO_AUTO_SIZE.TEXT_TO_FIT_SHAPE
|
|
17
|
+
|
|
18
|
+
The word-wrap setting of the text frame interacts with the auto-size setting to determine the
|
|
19
|
+
specific auto-sizing behavior.
|
|
20
|
+
|
|
21
|
+
Note that `TextFrame.auto_size` can also be set to |None|, which removes the auto size setting
|
|
22
|
+
altogether. This causes the setting to be inherited, either from the layout placeholder, in the
|
|
23
|
+
case of a placeholder shape, or from the theme.
|
|
24
|
+
|
|
25
|
+
MS API Name: `MsoAutoSize`
|
|
26
|
+
|
|
27
|
+
http://msdn.microsoft.com/en-us/library/office/ff865367(v=office.15).aspx
|
|
28
|
+
"""
|
|
29
|
+
|
|
30
|
+
NONE = (
|
|
31
|
+
0,
|
|
32
|
+
"No automatic sizing of the shape or text will be done.\n\nText can freely extend beyond"
|
|
33
|
+
" the horizontal and vertical edges of the shape bounding box.",
|
|
34
|
+
)
|
|
35
|
+
"""No automatic sizing of the shape or text will be done.
|
|
36
|
+
|
|
37
|
+
Text can freely extend beyond the horizontal and vertical edges of the shape bounding box.
|
|
38
|
+
"""
|
|
39
|
+
|
|
40
|
+
SHAPE_TO_FIT_TEXT = (
|
|
41
|
+
1,
|
|
42
|
+
"The shape height and possibly width are adjusted to fit the text.\n\nNote this setting"
|
|
43
|
+
" interacts with the TextFrame.word_wrap property setting. If word wrap is turned on,"
|
|
44
|
+
" only the height of the shape will be adjusted; soft line breaks will be used to fit the"
|
|
45
|
+
" text horizontally.",
|
|
46
|
+
)
|
|
47
|
+
"""The shape height and possibly width are adjusted to fit the text.
|
|
48
|
+
|
|
49
|
+
Note this setting interacts with the TextFrame.word_wrap property setting. If word wrap is
|
|
50
|
+
turned on, only the height of the shape will be adjusted; soft line breaks will be used to fit
|
|
51
|
+
the text horizontally.
|
|
52
|
+
"""
|
|
53
|
+
|
|
54
|
+
TEXT_TO_FIT_SHAPE = (
|
|
55
|
+
2,
|
|
56
|
+
"The font size is reduced as necessary to fit the text within the shape.",
|
|
57
|
+
)
|
|
58
|
+
"""The font size is reduced as necessary to fit the text within the shape."""
|
|
59
|
+
|
|
60
|
+
MIXED = (-2, "Return value only; indicates a combination of automatic sizing schemes are used.")
|
|
61
|
+
"""Return value only; indicates a combination of automatic sizing schemes are used."""
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
class MSO_TEXT_UNDERLINE_TYPE(BaseXmlEnum):
|
|
65
|
+
"""
|
|
66
|
+
Indicates the type of underline for text. Used with
|
|
67
|
+
:attr:`.Font.underline` to specify the style of text underlining.
|
|
68
|
+
|
|
69
|
+
Alias: ``MSO_UNDERLINE``
|
|
70
|
+
|
|
71
|
+
Example::
|
|
72
|
+
|
|
73
|
+
from pptx2.enum.text import MSO_UNDERLINE
|
|
74
|
+
|
|
75
|
+
run.font.underline = MSO_UNDERLINE.DOUBLE_LINE
|
|
76
|
+
|
|
77
|
+
MS API Name: `MsoTextUnderlineType`
|
|
78
|
+
|
|
79
|
+
http://msdn.microsoft.com/en-us/library/aa432699.aspx
|
|
80
|
+
"""
|
|
81
|
+
|
|
82
|
+
NONE = (0, "none", "Specifies no underline.")
|
|
83
|
+
"""Specifies no underline."""
|
|
84
|
+
|
|
85
|
+
DASH_HEAVY_LINE = (8, "dashHeavy", "Specifies a dash underline.")
|
|
86
|
+
"""Specifies a dash underline."""
|
|
87
|
+
|
|
88
|
+
DASH_LINE = (7, "dash", "Specifies a dash line underline.")
|
|
89
|
+
"""Specifies a dash line underline."""
|
|
90
|
+
|
|
91
|
+
DASH_LONG_HEAVY_LINE = (10, "dashLongHeavy", "Specifies a long heavy line underline.")
|
|
92
|
+
"""Specifies a long heavy line underline."""
|
|
93
|
+
|
|
94
|
+
DASH_LONG_LINE = (9, "dashLong", "Specifies a dashed long line underline.")
|
|
95
|
+
"""Specifies a dashed long line underline."""
|
|
96
|
+
|
|
97
|
+
DOT_DASH_HEAVY_LINE = (12, "dotDashHeavy", "Specifies a dot dash heavy line underline.")
|
|
98
|
+
"""Specifies a dot dash heavy line underline."""
|
|
99
|
+
|
|
100
|
+
DOT_DASH_LINE = (11, "dotDash", "Specifies a dot dash line underline.")
|
|
101
|
+
"""Specifies a dot dash line underline."""
|
|
102
|
+
|
|
103
|
+
DOT_DOT_DASH_HEAVY_LINE = (
|
|
104
|
+
14,
|
|
105
|
+
"dotDotDashHeavy",
|
|
106
|
+
"Specifies a dot dot dash heavy line underline.",
|
|
107
|
+
)
|
|
108
|
+
"""Specifies a dot dot dash heavy line underline."""
|
|
109
|
+
|
|
110
|
+
DOT_DOT_DASH_LINE = (13, "dotDotDash", "Specifies a dot dot dash line underline.")
|
|
111
|
+
"""Specifies a dot dot dash line underline."""
|
|
112
|
+
|
|
113
|
+
DOTTED_HEAVY_LINE = (6, "dottedHeavy", "Specifies a dotted heavy line underline.")
|
|
114
|
+
"""Specifies a dotted heavy line underline."""
|
|
115
|
+
|
|
116
|
+
DOTTED_LINE = (5, "dotted", "Specifies a dotted line underline.")
|
|
117
|
+
"""Specifies a dotted line underline."""
|
|
118
|
+
|
|
119
|
+
DOUBLE_LINE = (3, "dbl", "Specifies a double line underline.")
|
|
120
|
+
"""Specifies a double line underline."""
|
|
121
|
+
|
|
122
|
+
HEAVY_LINE = (4, "heavy", "Specifies a heavy line underline.")
|
|
123
|
+
"""Specifies a heavy line underline."""
|
|
124
|
+
|
|
125
|
+
SINGLE_LINE = (2, "sng", "Specifies a single line underline.")
|
|
126
|
+
"""Specifies a single line underline."""
|
|
127
|
+
|
|
128
|
+
WAVY_DOUBLE_LINE = (17, "wavyDbl", "Specifies a wavy double line underline.")
|
|
129
|
+
"""Specifies a wavy double line underline."""
|
|
130
|
+
|
|
131
|
+
WAVY_HEAVY_LINE = (16, "wavyHeavy", "Specifies a wavy heavy line underline.")
|
|
132
|
+
"""Specifies a wavy heavy line underline."""
|
|
133
|
+
|
|
134
|
+
WAVY_LINE = (15, "wavy", "Specifies a wavy line underline.")
|
|
135
|
+
"""Specifies a wavy line underline."""
|
|
136
|
+
|
|
137
|
+
WORDS = (1, "words", "Specifies underlining words.")
|
|
138
|
+
"""Specifies underlining words."""
|
|
139
|
+
|
|
140
|
+
MIXED = (-2, "", "Specifies a mix of underline types (read-only).")
|
|
141
|
+
"""Specifies a mix of underline types (read-only)."""
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
MSO_UNDERLINE = MSO_TEXT_UNDERLINE_TYPE
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
class MSO_VERTICAL_ANCHOR(BaseXmlEnum):
|
|
148
|
+
"""Specifies the vertical alignment of text in a text frame.
|
|
149
|
+
|
|
150
|
+
Used with the `.vertical_anchor` property of the |TextFrame| object. Note that the
|
|
151
|
+
`vertical_anchor` property can also have the value None, indicating there is no directly
|
|
152
|
+
specified vertical anchor setting and its effective value is inherited from its placeholder if
|
|
153
|
+
it has one or from the theme. |None| may also be assigned to remove an explicitly specified
|
|
154
|
+
vertical anchor setting.
|
|
155
|
+
|
|
156
|
+
MS API Name: `MsoVerticalAnchor`
|
|
157
|
+
|
|
158
|
+
http://msdn.microsoft.com/en-us/library/office/ff865255.aspx
|
|
159
|
+
"""
|
|
160
|
+
|
|
161
|
+
TOP = (1, "t", "Aligns text to top of text frame")
|
|
162
|
+
"""Aligns text to top of text frame"""
|
|
163
|
+
|
|
164
|
+
MIDDLE = (3, "ctr", "Centers text vertically")
|
|
165
|
+
"""Centers text vertically"""
|
|
166
|
+
|
|
167
|
+
BOTTOM = (4, "b", "Aligns text to bottom of text frame")
|
|
168
|
+
"""Aligns text to bottom of text frame"""
|
|
169
|
+
|
|
170
|
+
MIXED = (-2, "", "Return value only; indicates a combination of the other states.")
|
|
171
|
+
"""Return value only; indicates a combination of the other states."""
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
MSO_ANCHOR = MSO_VERTICAL_ANCHOR
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
class PP_PARAGRAPH_ALIGNMENT(BaseXmlEnum):
|
|
178
|
+
"""Specifies the horizontal alignment for one or more paragraphs.
|
|
179
|
+
|
|
180
|
+
Alias: `PP_ALIGN`
|
|
181
|
+
|
|
182
|
+
Example::
|
|
183
|
+
|
|
184
|
+
from pptx2.enum.text import PP_ALIGN
|
|
185
|
+
|
|
186
|
+
shape.paragraphs[0].alignment = PP_ALIGN.CENTER
|
|
187
|
+
|
|
188
|
+
MS API Name: `PpParagraphAlignment`
|
|
189
|
+
|
|
190
|
+
http://msdn.microsoft.com/en-us/library/office/ff745375(v=office.15).aspx
|
|
191
|
+
"""
|
|
192
|
+
|
|
193
|
+
CENTER = (2, "ctr", "Center align")
|
|
194
|
+
"""Center align"""
|
|
195
|
+
|
|
196
|
+
DISTRIBUTE = (
|
|
197
|
+
5,
|
|
198
|
+
"dist",
|
|
199
|
+
"Evenly distributes e.g. Japanese characters from left to right within a line",
|
|
200
|
+
)
|
|
201
|
+
"""Evenly distributes e.g. Japanese characters from left to right within a line"""
|
|
202
|
+
|
|
203
|
+
JUSTIFY = (
|
|
204
|
+
4,
|
|
205
|
+
"just",
|
|
206
|
+
"Justified, i.e. each line both begins and ends at the margin.\n\nSpacing between words"
|
|
207
|
+
" is adjusted such that the line exactly fills the width of the paragraph.",
|
|
208
|
+
)
|
|
209
|
+
"""Justified, i.e. each line both begins and ends at the margin.
|
|
210
|
+
|
|
211
|
+
Spacing between words is adjusted such that the line exactly fills the width of the paragraph.
|
|
212
|
+
"""
|
|
213
|
+
|
|
214
|
+
JUSTIFY_LOW = (7, "justLow", "Justify using a small amount of space between words.")
|
|
215
|
+
"""Justify using a small amount of space between words."""
|
|
216
|
+
|
|
217
|
+
LEFT = (1, "l", "Left aligned")
|
|
218
|
+
"""Left aligned"""
|
|
219
|
+
|
|
220
|
+
RIGHT = (3, "r", "Right aligned")
|
|
221
|
+
"""Right aligned"""
|
|
222
|
+
|
|
223
|
+
THAI_DISTRIBUTE = (6, "thaiDist", "Thai distributed")
|
|
224
|
+
"""Thai distributed"""
|
|
225
|
+
|
|
226
|
+
MIXED = (-2, "", "Multiple alignments are present in a set of paragraphs (read-only).")
|
|
227
|
+
"""Multiple alignments are present in a set of paragraphs (read-only)."""
|
|
228
|
+
|
|
229
|
+
|
|
230
|
+
PP_ALIGN = PP_PARAGRAPH_ALIGNMENT
|
pptx2/exc.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"""Exceptions used with python-pptx.
|
|
2
|
+
|
|
3
|
+
The base exception class is PythonPptxError.
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
from __future__ import annotations
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class PythonPptxError(Exception):
|
|
10
|
+
"""Generic error class."""
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class PackageNotFoundError(PythonPptxError):
|
|
14
|
+
"""
|
|
15
|
+
Raised when a package cannot be found at the specified path.
|
|
16
|
+
"""
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class InvalidXmlError(PythonPptxError):
|
|
20
|
+
"""
|
|
21
|
+
Raised when a value is encountered in the XML that is not valid according
|
|
22
|
+
to the schema.
|
|
23
|
+
"""
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class LintError(PythonPptxError):
|
|
27
|
+
"""Raised by :func:`~pptx2.compose.from_spec` when ``lint="raise"`` and the
|
|
28
|
+
linter detects errors in the generated presentation."""
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class FontMetricsWarning(UserWarning):
|
|
32
|
+
"""Warned when text is measured against a font that isn't the requested one.
|
|
33
|
+
|
|
34
|
+
:meth:`~pptx2.text.text.TextFrame.fit_text` measures with real font
|
|
35
|
+
metrics, so its "text will not overflow" guarantee only holds when the
|
|
36
|
+
requested family is actually installed. When it isn't, measurement falls
|
|
37
|
+
back to Pillow's bundled default font and the chosen size becomes a best
|
|
38
|
+
guess — usually close, but not something to trust for a display face.
|
|
39
|
+
|
|
40
|
+
Pass ``strict=True`` to raise instead, or install the font (see
|
|
41
|
+
:func:`pptx2.text.fonts.font_is_installed`).
|
|
42
|
+
"""
|
pptx2/formats.py
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
"""Number-format string helpers for chart data labels and table cells.
|
|
2
|
+
|
|
3
|
+
Setting a chart data label to a currency or percentage format
|
|
4
|
+
otherwise requires Excel's format-string syntax verbatim::
|
|
5
|
+
|
|
6
|
+
plot.data_labels.number_format = '"$"#,##0'
|
|
7
|
+
|
|
8
|
+
That syntax is leaky, easy to typo, and hostile to anyone who hasn't
|
|
9
|
+
written Excel macros. The helpers here return the right format string
|
|
10
|
+
for the common semantic choices and keep Excel syntax behind the
|
|
11
|
+
abstraction:
|
|
12
|
+
|
|
13
|
+
from pptx2.formats import currency, percent, date, decimal, scientific
|
|
14
|
+
|
|
15
|
+
plot.data_labels.number_format = currency("$", decimals=0) # "$"#,##0
|
|
16
|
+
plot.data_labels.number_format = currency("£", decimals=2) # "£"#,##0.00
|
|
17
|
+
plot.data_labels.number_format = percent(decimals=1) # 0.0%
|
|
18
|
+
plot.data_labels.number_format = decimal(decimals=2) # #,##0.00
|
|
19
|
+
plot.data_labels.number_format = date("YYYY-MM-DD") # yyyy-MM-dd
|
|
20
|
+
plot.data_labels.number_format = scientific(decimals=2) # 0.00E+00
|
|
21
|
+
|
|
22
|
+
The strings are plain ``str`` so they round-trip through
|
|
23
|
+
``data_labels.number_format`` exactly like a hand-written format,
|
|
24
|
+
and remain editable through Excel's "Format Cells" dialog.
|
|
25
|
+
"""
|
|
26
|
+
|
|
27
|
+
from __future__ import annotations
|
|
28
|
+
|
|
29
|
+
from typing import Final
|
|
30
|
+
|
|
31
|
+
__all__ = ("currency", "percent", "decimal", "scientific", "date", "thousands")
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
_DATE_TOKEN_MAP: Final[dict[str, str]] = {
|
|
35
|
+
# Map common UI-style date tokens to Excel's lowercase form.
|
|
36
|
+
# ``YYYY``-style is what most authoring guides write; Excel
|
|
37
|
+
# canonicalises to lowercase except for ``M`` vs ``m`` (where
|
|
38
|
+
# case decides "month" vs "minute"). This helper preserves that
|
|
39
|
+
# distinction by leaving ``M`` / ``MM`` / ``MMM`` / ``MMMM``
|
|
40
|
+
# capitalised in the output — Excel still reads them as months
|
|
41
|
+
# in a date context.
|
|
42
|
+
"YYYY": "yyyy",
|
|
43
|
+
"YY": "yy",
|
|
44
|
+
"DDDD": "dddd",
|
|
45
|
+
"DDD": "ddd",
|
|
46
|
+
"DD": "dd",
|
|
47
|
+
"D": "d",
|
|
48
|
+
"HH": "hh",
|
|
49
|
+
"H": "h",
|
|
50
|
+
"SS": "ss",
|
|
51
|
+
"S": "s",
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def currency(symbol: str = "$", *, decimals: int = 0) -> str:
|
|
56
|
+
"""Return a currency format string with `symbol` and `decimals` digits.
|
|
57
|
+
|
|
58
|
+
`symbol` is wrapped in double quotes so multi-character codes
|
|
59
|
+
(``"USD "``, ``"EUR "``) work too. ``decimals`` must be ``>= 0``.
|
|
60
|
+
"""
|
|
61
|
+
if decimals < 0:
|
|
62
|
+
raise ValueError(f"decimals must be >= 0; got {decimals}")
|
|
63
|
+
if not isinstance(symbol, str): # type: ignore[redundant-expr]
|
|
64
|
+
raise TypeError(f"symbol must be str; got {type(symbol).__name__}")
|
|
65
|
+
base = f'"{symbol}"#,##0'
|
|
66
|
+
if decimals == 0:
|
|
67
|
+
return base
|
|
68
|
+
return base + "." + ("0" * decimals)
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def percent(*, decimals: int = 0) -> str:
|
|
72
|
+
"""Return a percentage format string.
|
|
73
|
+
|
|
74
|
+
A value of ``0.27`` renders as ``27%`` (no decimals) or ``27.0%``
|
|
75
|
+
(one decimal). Excel multiplies by 100 implicitly when the
|
|
76
|
+
format ends with ``%``, matching user expectation.
|
|
77
|
+
"""
|
|
78
|
+
if decimals < 0:
|
|
79
|
+
raise ValueError(f"decimals must be >= 0; got {decimals}")
|
|
80
|
+
if decimals == 0:
|
|
81
|
+
return "0%"
|
|
82
|
+
return "0." + ("0" * decimals) + "%"
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def decimal(*, decimals: int = 2, thousands_sep: bool = True) -> str:
|
|
86
|
+
"""Return a fixed-decimal numeric format.
|
|
87
|
+
|
|
88
|
+
``decimals=2, thousands_sep=True`` → ``#,##0.00``;
|
|
89
|
+
``decimals=0, thousands_sep=False`` → ``0``.
|
|
90
|
+
"""
|
|
91
|
+
if decimals < 0:
|
|
92
|
+
raise ValueError(f"decimals must be >= 0; got {decimals}")
|
|
93
|
+
base = "#,##0" if thousands_sep else "0"
|
|
94
|
+
if decimals == 0:
|
|
95
|
+
return base
|
|
96
|
+
return base + "." + ("0" * decimals)
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def thousands() -> str:
|
|
100
|
+
"""Return ``#,##0`` — integer with thousands separator."""
|
|
101
|
+
return "#,##0"
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
def scientific(*, decimals: int = 2) -> str:
|
|
105
|
+
"""Return a scientific-notation format with `decimals` digits."""
|
|
106
|
+
if decimals < 0:
|
|
107
|
+
raise ValueError(f"decimals must be >= 0; got {decimals}")
|
|
108
|
+
if decimals == 0:
|
|
109
|
+
return "0E+00"
|
|
110
|
+
return "0." + ("0" * decimals) + "E+00"
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
def date(pattern: str = "YYYY-MM-DD") -> str:
|
|
114
|
+
"""Return an Excel-style date format from a UI-style `pattern`.
|
|
115
|
+
|
|
116
|
+
Accepts the upper-case tokens authors typically write
|
|
117
|
+
(``YYYY``, ``MM``, ``DD``, ``HH``, ``SS``) and lowercases them
|
|
118
|
+
where Excel's parser needs lowercase. ``M`` / ``MM`` / ``MMM``
|
|
119
|
+
/ ``MMMM`` (month) are intentionally left capitalised — Excel
|
|
120
|
+
reads them as months in a date context, while their lowercase
|
|
121
|
+
form is "minutes".
|
|
122
|
+
|
|
123
|
+
Examples::
|
|
124
|
+
|
|
125
|
+
date() # "yyyy-MM-dd" (default ISO date)
|
|
126
|
+
date("YYYY-MM-DD HH:SS") # "yyyy-MM-dd hh:ss" -- "MM" stays
|
|
127
|
+
# capitalised so Excel reads it
|
|
128
|
+
# as month, not minutes
|
|
129
|
+
date("MMM YYYY") # "MMM yyyy"
|
|
130
|
+
"""
|
|
131
|
+
if not isinstance(pattern, str): # type: ignore[redundant-expr]
|
|
132
|
+
raise TypeError(f"pattern must be str; got {type(pattern).__name__}")
|
|
133
|
+
out = pattern
|
|
134
|
+
# Apply replacements longest-first so YYYY isn't shadowed by YY.
|
|
135
|
+
for src, dst in sorted(
|
|
136
|
+
_DATE_TOKEN_MAP.items(), key=lambda kv: -len(kv[0])
|
|
137
|
+
):
|
|
138
|
+
out = out.replace(src, dst)
|
|
139
|
+
return out
|