Skip to content

Commit 75c0302

Browse files
author
Steve Canny
committed
oxml: extract OxmlElement() to pptx.oxml
1 parent 38f18c7 commit 75c0302

File tree

7 files changed

+27
-24
lines changed

7 files changed

+27
-24
lines changed

docx/oxml/__init__.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
from lxml import etree
1111

12-
from .ns import nsmap
12+
from .ns import NamespacePrefixedTag, nsmap
1313

1414

1515
# configure XML parser
@@ -40,6 +40,21 @@ def register_custom_element_class(tag, cls):
4040
namespace[tagroot] = cls
4141

4242

43+
def OxmlElement(nsptag_str, attrs=None, nsmap=None):
44+
"""
45+
Return a 'loose' lxml element having the tag specified by *nsptag_str*.
46+
*nsptag_str* must contain the standard namespace prefix, e.g. 'a:tbl'.
47+
The resulting element is an instance of the custom element class for this
48+
tag name if one is defined. A dictionary of attribute values may be
49+
provided as *attrs*; they are set if present.
50+
"""
51+
nsptag = NamespacePrefixedTag(nsptag_str)
52+
_nsmap = nsptag.nsmap if nsmap is None else nsmap
53+
return oxml_parser.makeelement(
54+
nsptag.clark_name, attrib=attrs, nsmap=_nsmap
55+
)
56+
57+
4358
# ===========================================================================
4459
# custom element class mappings
4560
# ===========================================================================

docx/oxml/parts/numbering.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
Custom element classes related to the numbering part
55
"""
66

7-
from ..shared import CT_DecimalNumber, OxmlBaseElement, OxmlElement
7+
from .. import OxmlElement
8+
from ..shared import CT_DecimalNumber, OxmlBaseElement
89
from ..ns import nsmap, qn
910

1011

docx/oxml/shape.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
Custom element classes for shape-related elements like ``<w:inline>``
55
"""
66

7-
from .shared import OxmlBaseElement, OxmlElement
7+
from . import OxmlElement
8+
from .shared import OxmlBaseElement
89
from ..shared import Emu
910
from .ns import nsmap, nspfxmap, qn
1011

docx/oxml/shared.py

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,16 @@
1010

1111
import re
1212

13-
from . import oxml_parser
13+
from . import OxmlElement
1414
from .exceptions import ValidationError
15-
from .ns import NamespacePrefixedTag, qn
15+
from .ns import qn
1616

1717

1818
# ===========================================================================
1919
# utility functions
2020
# ===========================================================================
2121

2222

23-
def OxmlElement(nsptag_str, attrs=None, nsmap=None):
24-
"""
25-
Return a 'loose' lxml element having the tag specified by *nsptag_str*.
26-
*nsptag_str* must contain the standard namespace prefix, e.g. 'a:tbl'.
27-
The resulting element is an instance of the custom element class for this
28-
tag name if one is defined. A dictionary of attribute values may be
29-
provided as *attrs*; they are set if present.
30-
"""
31-
nsptag = NamespacePrefixedTag(nsptag_str)
32-
_nsmap = nsptag.nsmap if nsmap is None else nsmap
33-
return oxml_parser.makeelement(
34-
nsptag.clark_name, attrib=attrs, nsmap=_nsmap
35-
)
36-
37-
3823
def serialize_for_reading(element):
3924
"""
4025
Serialize *element* to human-readable XML suitable for tests. No XML

docx/oxml/table.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66

77
from __future__ import absolute_import, print_function, unicode_literals
88

9+
from . import OxmlElement
910
from .exceptions import ValidationError
1011
from .ns import qn
11-
from .shared import CT_String, OxmlBaseElement, OxmlElement
12+
from .shared import CT_String, OxmlBaseElement
1213
from .text import CT_P
1314

1415

docx/oxml/text.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
(CT_R).
66
"""
77

8-
from . import parse_xml
8+
from . import parse_xml, OxmlElement
99
from ..enum.text import WD_UNDERLINE
1010
from .ns import nsdecls, qn
1111
from .parts.numbering import CT_NumPr
12-
from .shared import CT_String, OxmlBaseElement, OxmlElement
12+
from .shared import CT_String, OxmlBaseElement
1313

1414

1515
class CT_Br(OxmlBaseElement):

docx/package.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,6 @@ def image_partname(n):
110110
return PackURI('/word/media/image%d.%s' % (n, ext))
111111
used_numbers = [image_part.partname.idx for image_part in self]
112112
for n in range(1, len(self)+1):
113-
if not n in used_numbers:
113+
if n not in used_numbers:
114114
return image_partname(n)
115115
return image_partname(len(self)+1)

0 commit comments

Comments
 (0)