-
Notifications
You must be signed in to change notification settings - Fork 540
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added a:fld type to paragraphs for page numbers and datetimes #797
base: master
Are you sure you want to change the base?
Changes from 1 commit
b5630e3
ff11dcb
0af82a9
9eadcf3
7d08858
425633b
09c893f
682e6ea
d65276c
0549dec
8964257
7de33d6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,8 @@ | |
ST_TextTypeface, | ||
ST_TextWrappingType, | ||
XsdBoolean, | ||
XsdString, | ||
ST_FieldType, | ||
) | ||
from pptx.oxml.xmlchemy import ( | ||
BaseOxmlElement, | ||
|
@@ -324,8 +326,10 @@ class CT_TextField(BaseOxmlElement): | |
<a:fld> field element, for either a slide number or date field | ||
""" | ||
|
||
id = RequiredAttribute("id", XsdString) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. id is a python built-in function. Might use a different name. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I recognize that we are shadowing the id built-in, but since we are inside of a class, and are unlikely to need the id function here, I think the readability makes it worth it. There is also some precedent in the codebase for this: the CT_SlideId class in pptx/oxml/presentation.py, the CT_Connection class in pptx/oxml/shapes/connector.py, and CT_NonVisualDrawingProps in pptx/oxml/shapes/shared.py all have attributes named id. |
||
rPr = ZeroOrOne("a:rPr", successors=("a:pPr", "a:t")) | ||
t = ZeroOrOne("a:t", successors=()) | ||
type = OptionalAttribute("type", ST_FieldType) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. type is a python built-in function. Might use a different name. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See comment above re: id. There is a precedent for this as well in the CT_Placeholder class in pptx/oxml/shapes/shared.py |
||
|
||
@property | ||
def text(self): | ||
|
@@ -338,6 +342,28 @@ def text(self): | |
text = t.text | ||
return to_unicode(text) if text is not None else "" | ||
|
||
@text.setter | ||
def text(self, str): | ||
"""*str* is unicode value to replace run text.""" | ||
t = self.t | ||
|
||
if t is None: | ||
self.get_or_add_t() | ||
|
||
self.t.text = self._escape_ctrl_chars(str) | ||
|
||
@staticmethod | ||
def _escape_ctrl_chars(s): | ||
"""Return str after replacing each control character with a plain-text escape. | ||
|
||
For example, a BEL character (x07) would appear as "_x0007_". Horizontal-tab | ||
(x09) and line-feed (x0A) are not escaped. All other characters in the range | ||
x00-x1F are escaped. | ||
""" | ||
return re.sub( | ||
r"([\x00-\x08\x0B-\x1F])", lambda match: "_x%04X_" % ord(match.group(1)), s | ||
) | ||
|
||
|
||
class CT_TextFont(BaseOxmlElement): | ||
""" | ||
|
@@ -379,6 +405,7 @@ class CT_TextParagraph(BaseOxmlElement): | |
pPr = ZeroOrOne("a:pPr", successors=("a:r", "a:br", "a:fld", "a:endParaRPr")) | ||
r = ZeroOrMore("a:r", successors=("a:endParaRPr",)) | ||
br = ZeroOrMore("a:br", successors=("a:endParaRPr",)) | ||
fld = ZeroOrMore("a:fld", successors=("a:endParaRPr",)) | ||
endParaRPr = ZeroOrOne("a:endParaRPr", successors=()) | ||
|
||
def add_br(self): | ||
|
@@ -387,11 +414,20 @@ def add_br(self): | |
""" | ||
return self._add_br() | ||
|
||
def add_fld(self, text=None): | ||
f = self._add_fld() | ||
|
||
if text: | ||
f.text = text | ||
return f | ||
|
||
|
||
def add_r(self, text=None): | ||
""" | ||
Return a newly appended <a:r> element. | ||
""" | ||
r = self._add_r() | ||
|
||
if text: | ||
r.text = text | ||
return r | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like these are redundant additions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I removed the redundant lines