This repository was archived by the owner on Jan 7, 2024. It is now read-only.
forked from holli-holzer/python-docx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocument.py
More file actions
110 lines (78 loc) · 3.55 KB
/
document.py
File metadata and controls
110 lines (78 loc) · 3.55 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# encoding: utf-8
"""
Step implementations for document-related features
"""
from __future__ import absolute_import, print_function, unicode_literals
from behave import given, then, when
from docx import Document
from docx.enum.section import WD_ORIENT, WD_SECTION
from docx.parts.document import Sections
from docx.section import Section
from helpers import test_docx
# given ===================================================
@given('a document having three sections')
def given_a_document_having_three_sections(context):
context.document = Document(test_docx('doc-access-sections'))
@given('a section collection')
def given_a_section_collection(context):
document = Document(test_docx('doc-access-sections'))
context.sections = document.sections
@given('a single-section document having portrait layout')
def given_a_single_section_document_having_portrait_layout(context):
context.document = Document(test_docx('doc-add-section'))
section = context.document.sections[-1]
context.original_dimensions = (section.page_width, section.page_height)
# when ====================================================
@when('I add an even-page section to the document')
def when_I_add_an_even_page_section_to_the_document(context):
context.section = context.document.add_section(WD_SECTION.EVEN_PAGE)
@when('I change the new section layout to landscape')
def when_I_change_the_new_section_layout_to_landscape(context):
new_height, new_width = context.original_dimensions
section = context.section
section.orientation = WD_ORIENT.LANDSCAPE
section.page_width = new_width
section.page_height = new_height
# then ====================================================
@then('I can access a section by index')
def then_I_can_access_a_section_by_index(context):
sections = context.sections
for idx in range(3):
section = sections[idx]
assert isinstance(section, Section)
@then('I can access the section collection of the document')
def then_I_can_access_the_section_collection_of_the_document(context):
sections = context.document.sections
msg = 'document.sections not instance of Sections'
assert isinstance(sections, Sections), msg
@then('I can iterate over the sections')
def then_I_can_iterate_over_the_sections(context):
sections = context.sections
actual_count = 0
for section in sections:
actual_count += 1
assert isinstance(section, Section)
assert actual_count == 3
@then('the document has two sections')
def then_the_document_has_two_sections(context):
assert len(context.document.sections) == 2
@then('the first section is portrait')
def then_the_first_section_is_portrait(context):
first_section = context.document.sections[0]
expected_width, expected_height = context.original_dimensions
assert first_section.orientation == WD_ORIENT.PORTRAIT
assert first_section.page_width == expected_width
assert first_section.page_height == expected_height
@then('the length of the section collection is 3')
def then_the_length_of_the_section_collection_is_3(context):
sections = context.document.sections
assert len(sections) == 3, (
'expected len(sections) of 2, got %s' % len(sections)
)
@then('the second section is landscape')
def then_the_second_section_is_landscape(context):
new_section = context.document.sections[-1]
expected_height, expected_width = context.original_dimensions
assert new_section.orientation == WD_ORIENT.LANDSCAPE
assert new_section.page_width == expected_width
assert new_section.page_height == expected_height