Skip to content

Commit a9176b3

Browse files
Initial work on Headers (python-openxml#104)
1 parent bc34ec9 commit a9176b3

4 files changed

Lines changed: 118 additions & 2 deletions

File tree

docx/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from docx.parts.image import ImagePart
1616
from docx.parts.numbering import NumberingPart
1717
from docx.parts.styles import StylesPart
18-
18+
from docx.parts.section import HeaderPart,FooterPart
1919

2020
def part_class_selector(content_type, reltype):
2121
if reltype == RT.IMAGE:
@@ -28,7 +28,8 @@ def part_class_selector(content_type, reltype):
2828
PartFactory.part_type_for[CT.WML_DOCUMENT_MAIN] = DocumentPart
2929
PartFactory.part_type_for[CT.WML_NUMBERING] = NumberingPart
3030
PartFactory.part_type_for[CT.WML_STYLES] = StylesPart
31-
31+
PartFactory.part_type_for[CT.WML_HEADER] = HeaderPart
32+
PartFactory.part_type_for[CT.WML_FOOTER] = FooterPart
3233
del (
3334
CT, CorePropertiesPart, DocumentPart, NumberingPart, PartFactory,
3435
StylesPart, part_class_selector

docx/document.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ def __init__(self, element, part):
2828
self._part = part
2929
self.__body = None
3030

31+
def add_header(self,text='',header_type='first'):
32+
return
33+
3134
def add_heading(self, text='', level=1):
3235
"""
3336
Return a heading paragraph newly added to the end of the document,

docx/parts/header.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# encoding: utf-8
2+
3+
"""
4+
Provides HeaderPart and related objects
5+
"""
6+
7+
from __future__ import (
8+
absolute_import, division, print_function, unicode_literals
9+
)
10+
11+
from ..opc.part import XmlPart
12+
from ..oxml.shared import oxml_fromstring
13+
from ..shared import lazyproperty
14+
15+
16+
class HeaderPart(XmlPart):
17+
"""
18+
Proxy for the styles.xml part containing style definitions for a document
19+
or glossary.
20+
"""
21+
def __init__(self, partname, content_type, element, package):
22+
super(HeaderPart, self).__init__(
23+
partname, content_type, element=element, package=package
24+
)
25+
26+
@classmethod
27+
def load(cls, partname, content_type, blob, package):
28+
"""
29+
Provides PartFactory interface for loading a styles part from a WML
30+
package.
31+
"""
32+
styles_elm = oxml_fromstring(blob)
33+
return cls(partname, content_type, styles_elm, package)
34+
35+
36+
@classmethod
37+
def new(cls):
38+
"""
39+
Return newly created empty header part, containing only the root
40+
``<w:???>`` element.
41+
"""
42+
raise NotImplementedError
43+
44+
@lazyproperty
45+
def header(self):
46+
"""
47+
The |_Header| instance containing the header (<w:???> element
48+
proxies) for this header part.
49+
"""
50+
return _Header(self._element)
51+
52+
53+
class _Header(object):
54+
"""
55+
FIX THIS >>> Collection of |_Header| instances corresponding to the ``<w:???>``
56+
elements in a header part.
57+
"""
58+
def __init__(self, header_elm):
59+
super(_Header, self).__init__()
60+
self._header_elm = header_elm
61+
62+
def __len__(self):
63+
return len(self._styles_elm.header_lst)

docx/parts/section.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# encoding: utf-8
2+
3+
"""
4+
Provides HeaderPart and related objects
5+
"""
6+
7+
from __future__ import (
8+
absolute_import, division, print_function, unicode_literals
9+
)
10+
11+
from ..opc.part import XmlPart
12+
from ..shared import lazyproperty
13+
from docx.blkcntnr import BlockItemContainer
14+
15+
16+
class HeaderPart(XmlPart):
17+
"""
18+
Proxy for the styles.xml part containing style definitions for a document
19+
or glossary.
20+
"""
21+
22+
@lazyproperty
23+
def header(self):
24+
"""
25+
The |_Header| instance containing the header (<w:???> element
26+
proxies) for this header part.
27+
"""
28+
return _Header(self._element, self)
29+
30+
31+
class FooterPart(XmlPart):
32+
""""""
33+
34+
@lazyproperty
35+
def footer(self):
36+
""""""
37+
return _Footer(self._element, self)
38+
39+
40+
class _Header(BlockItemContainer):
41+
def __init__(self, header_elm, parent):
42+
super(_Header, self).__init__(header_elm, parent)
43+
self._header_elm = header_elm
44+
45+
46+
class _Footer(BlockItemContainer):
47+
def __init__(self, footer_elm, parent):
48+
super(_Footer, self).__init__(footer_elm, parent)
49+
self._footer_elm = footer_elm

0 commit comments

Comments
 (0)