forked from eupharis/python-docx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_header.py
More file actions
74 lines (56 loc) · 2.38 KB
/
test_header.py
File metadata and controls
74 lines (56 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import pytest
from .unitutil.file import absjoin, test_file_dir
from docx.api import Document
from docx.oxml.header import CT_Hdr
from docx.oxml.ns import qn
from docx.opc.constants import CONTENT_TYPE as CT, RELATIONSHIP_TYPE as RT
from docx.opc.part import XmlPart
dir_pkg_path = absjoin(test_file_dir, 'expanded_docx')
class DescribeHeaderLoad(object):
def it_has_part_as_header_part(self):
document = Document(dir_pkg_path)
header_part_exists = False
for rel_id, part in document.part.related_parts.items():
if part.content_type == CT.WML_HEADER:
header_part_exists = True
assert isinstance(part, XmlPart)
assert header_part_exists
def it_has_rel_as_header_rel(self):
document = Document(dir_pkg_path)
header_rel_exists = False
for rel_id, rel in document.part.rels.items():
if rel.reltype == RT.HEADER:
header_rel_exists = True
assert header_rel_exists
class DescribeRemoveHeader(object):
def it_removes_header_part(self):
document = Document(dir_pkg_path)
document.remove_headers()
for rel_id, part in document.part.related_parts.items():
assert part.content_type != CT.WML_HEADER
header_elm_tag = 'w:headerReference'
sentinel_sectPr = document._body._body.get_or_add_sectPr()
header_elms = sentinel_sectPr.findall(qn(header_elm_tag))
assert len(header_elms) == 0
class DescribeAddHeader(object):
pytest.skip('todo actually add add_header methods')
def it_adds_to_doc_without_header(self):
document = Document(dir_pkg_path)
sentinel_sectPr = document.sections[0]
header_elm_tag = 'w:headerReference'
header = sentinel_sectPr.add_header()
header_elms = sentinel_sectPr.findall(qn(header_elm_tag))
assert len(header_elms) == 1
assert header
assert len(header.paragraphs) == 0
header.add_paragraph('foobar')
assert len(header.paragraphs) == 1
# import uuid
# random_name = uuid.uuid4().hex
# finish_path = '{}.docx'.format(random_name)
# document.save(finish_path)
# print 'file {} header added!'.format(finish_path)
class DescribeCTHdr(object):
def it_creates_an_element_of_type_w_hdr(self):
header = CT_Hdr.new()
assert header.tag.endswith('hdr')