Skip to content

Commit da75fcf

Browse files
committedJun 10, 2019
fix: tweak setup.py handling of template directory
Also some pending documentation updates and some Black changes.
1 parent 90fc91c commit da75fcf

File tree

5 files changed

+37
-46
lines changed

5 files changed

+37
-46
lines changed
 

‎docs/user/hdrftr.rst

+9-16
Original file line numberDiff line numberDiff line change
@@ -77,24 +77,13 @@ Adding "zoned" header content
7777
-----------------------------
7878

7979
A header with multiple "zones" is often accomplished using carefully placed tab stops.
80-
The required tab-stops for a center and right-aligned "zone" are part of the latent
81-
``Header`` style in Word, but that style is not present in the default ``python-docx``
82-
template and will need to be added::
8380

84-
>>> from docx.enum.style import WD_STYLE_TYPE
85-
>>> from docx.enum.text import WD_TAB_ALIGNMENT
86-
>>> styles = document.styles
87-
>>> style = styles.add_style("Header", WD_STYLE_TYPE.PARAGRAPH)
88-
>>> style.base_style = styles["Normal"]
89-
>>> tab_stops = style.paragraph_format.tab_stops
90-
>>> tab_stops.add_tab_stop(Inches(3.25), WD_TAB_ALIGNMENT.CENTER)
91-
>>> tab_stops.add_tab_stop(Inches(6.5), WD_TAB_ALIGNMENT.RIGHT)
81+
The required tab-stops for a center and right-aligned "zone" are part of the ``Header``
82+
and ``Footer`` styles in Word. If you're using a custom template rather than the
83+
`python-docx` default, it probably makes sense to define that style in your template.
9284

93-
If you're using a custom template rather than the `python-docx` default, it probably
94-
makes sense to define that style in your template.
95-
96-
Once the ``Header`` style is present, tabs are used to separate left, center, and
97-
right-aligned header content::
85+
Inserted tab characters (``"\t"``) are used to separate left, center, and right-aligned
86+
header content::
9887

9988
>>> paragraph = header.paragraphs[0]
10089
>>> paragraph.text = "Left Text\tCenter Text\tRight Text"
@@ -103,6 +92,10 @@ right-aligned header content::
10392
.. image:: /_static/img/hdrftr-02.png
10493
:scale: 75%
10594

95+
The ``Header`` style is automatically applied to a new header, so the third line just
96+
above (applying the ``Header`` style) is unnecessary in this case, but included here to
97+
illustrate the general case.
98+
10699

107100
Removing a header
108101
-----------------

‎docx/compat.py

+5-9
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44
Provides Python 2/3 compatibility objects
55
"""
66

7-
from __future__ import (
8-
absolute_import, division, print_function, unicode_literals
9-
)
7+
from __future__ import absolute_import, division, print_function, unicode_literals
108

119
import sys
1210

@@ -16,12 +14,11 @@
1614

1715
if sys.version_info >= (3, 0):
1816

17+
from collections.abc import Sequence
1918
from io import BytesIO
2019

2120
def is_string(obj):
22-
"""
23-
Return True if *obj* is a string, False otherwise.
24-
"""
21+
"""Return True if *obj* is a string, False otherwise."""
2522
return isinstance(obj, str)
2623

2724
Unicode = str
@@ -32,12 +29,11 @@ def is_string(obj):
3229

3330
else:
3431

32+
from collections import Sequence # noqa
3533
from StringIO import StringIO as BytesIO # noqa
3634

3735
def is_string(obj):
38-
"""
39-
Return True if *obj* is a string, False otherwise.
40-
"""
36+
"""Return True if *obj* is a string, False otherwise."""
4137
return isinstance(obj, basestring)
4238

4339
Unicode = unicode

‎docx/oxml/ns.py

+16-15
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,22 @@
88

99

1010
nsmap = {
11-
"a": ("http://schemas.openxmlformats.org/drawingml/2006/main"),
12-
"c": ("http://schemas.openxmlformats.org/drawingml/2006/chart"),
13-
"cp": ("http://schemas.openxmlformats.org/package/2006/metadata/core-properties"),
14-
"dc": ("http://purl.org/dc/elements/1.1/"),
15-
"dcmitype": ("http://purl.org/dc/dcmitype/"),
16-
"dcterms": ("http://purl.org/dc/terms/"),
17-
"dgm": ("http://schemas.openxmlformats.org/drawingml/2006/diagram"),
18-
"m": ("http://schemas.openxmlformats.org/officeDocument/2006/math"),
19-
"pic": ("http://schemas.openxmlformats.org/drawingml/2006/picture"),
20-
"r": ("http://schemas.openxmlformats.org/officeDocument/2006/relationships"),
21-
"sl": ("http://schemas.openxmlformats.org/schemaLibrary/2006/main"),
22-
"w": ("http://schemas.openxmlformats.org/wordprocessingml/2006/main"),
23-
"wp": ("http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing"),
24-
"xml": ("http://www.w3.org/XML/1998/namespace"),
25-
"xsi": ("http://www.w3.org/2001/XMLSchema-instance"),
11+
"a": "http://schemas.openxmlformats.org/drawingml/2006/main",
12+
"c": "http://schemas.openxmlformats.org/drawingml/2006/chart",
13+
"cp": "http://schemas.openxmlformats.org/package/2006/metadata/core-properties",
14+
"dc": "http://purl.org/dc/elements/1.1/",
15+
"dcmitype": "http://purl.org/dc/dcmitype/",
16+
"dcterms": "http://purl.org/dc/terms/",
17+
"dgm": "http://schemas.openxmlformats.org/drawingml/2006/diagram",
18+
"m": "http://schemas.openxmlformats.org/officeDocument/2006/math",
19+
"pic": "http://schemas.openxmlformats.org/drawingml/2006/picture",
20+
"r": "http://schemas.openxmlformats.org/officeDocument/2006/relationships",
21+
"sl": "http://schemas.openxmlformats.org/schemaLibrary/2006/main",
22+
"w": "http://schemas.openxmlformats.org/wordprocessingml/2006/main",
23+
'w14': "http://schemas.microsoft.com/office/word/2010/wordml",
24+
"wp": "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing",
25+
"xml": "http://www.w3.org/XML/1998/namespace",
26+
"xsi": "http://www.w3.org/2001/XMLSchema-instance",
2627
}
2728

2829
pfxmap = dict((value, key) for key, value in nsmap.items())

‎docx/section.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@
44

55
from __future__ import absolute_import, division, print_function, unicode_literals
66

7-
from collections import Sequence
8-
97
from docx.blkcntnr import BlockItemContainer
8+
from docx.compat import Sequence
109
from docx.enum.section import WD_HEADER_FOOTER
1110
from docx.shared import lazyproperty
1211

@@ -81,7 +80,7 @@ def even_page_footer(self):
8180
The content of this footer definition is ignored unless the document setting
8281
:attr:`~.Settings.odd_and_even_pages_header_footer` is set True.
8382
"""
84-
return _Footer(self._sectPr, self._document_part, WD_HEADER_FOOTER.EVEN_PAGE)
83+
return _Footer(self._sectPr, self._document_part, WD_HEADER_FOOTER.EVEN_PAGE)
8584

8685
@property
8786
def even_page_header(self):
@@ -394,7 +393,8 @@ def _prior_headerfooter(self):
394393
"""|_Footer| proxy on prior sectPr element or None if this is first section."""
395394
preceding_sectPr = self._sectPr.preceding_sectPr
396395
return (
397-
None if preceding_sectPr is None
396+
None
397+
if preceding_sectPr is None
398398
else _Footer(preceding_sectPr, self._document_part, self._hdrftr_index)
399399
)
400400

@@ -437,6 +437,7 @@ def _prior_headerfooter(self):
437437
"""|_Header| proxy on prior sectPr element or None if this is first section."""
438438
preceding_sectPr = self._sectPr.preceding_sectPr
439439
return (
440-
None if preceding_sectPr is None
440+
None
441+
if preceding_sectPr is None
441442
else _Header(preceding_sectPr, self._document_part, self._hdrftr_index)
442443
)

‎setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def text_of(relpath):
3535
URL = 'https://github.com/python-openxml/python-docx'
3636
LICENSE = text_of('LICENSE')
3737
PACKAGES = find_packages(exclude=['tests', 'tests.*'])
38-
PACKAGE_DATA = {'docx': ['templates/*']}
38+
PACKAGE_DATA = {'docx': ['templates/*.xml', 'templates/*.docx']}
3939

4040
INSTALL_REQUIRES = ['lxml>=2.3.2']
4141
TEST_SUITE = 'tests'

0 commit comments

Comments
 (0)