X Tutup
Skip to content

Commit 9523610

Browse files
author
Steve Canny
committed
acpt: add doc-add-section.feature
1 parent a7d9915 commit 9523610

File tree

3 files changed

+62
-1
lines changed

3 files changed

+62
-1
lines changed

features/doc-add-section.feature

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
Feature: Add a document section
2+
In order to change page layout mid-document
3+
As a developer using python-docx
4+
I need a way to add a new section to a document
5+
6+
7+
@wip
8+
Scenario: Add a landscape section to a portrait document
9+
Given a single-section document having portrait layout
10+
When I add an even-page section to the document
11+
And I change the new section layout to landscape
12+
Then the document has two sections
13+
And the first section is portrait
14+
And the second section is landscape

features/steps/document.py

Lines changed: 48 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 behave import given, then
9+
from behave import given, then, when
1010

1111
from docx import Document
12+
from docx.enum.section import WD_ORIENT, WD_SECTION
1213
from docx.parts.document import Sections
1314
from docx.section import Section
1415

@@ -28,6 +29,29 @@ def given_a_section_collection(context):
2829
context.sections = document.sections
2930

3031

32+
@given('a single-section document having portrait layout')
33+
def given_a_single_section_document_having_portrait_layout(context):
34+
context.document = Document(test_docx('doc-add-section'))
35+
section = context.document.sections[-1]
36+
context.original_dimensions = (section.page_width, section.page_height)
37+
38+
39+
# when ====================================================
40+
41+
@when('I add an even-page section to the document')
42+
def when_I_add_an_even_page_section_to_the_document(context):
43+
context.section = context.document.add_section(WD_SECTION.EVEN_PAGE)
44+
45+
46+
@when('I change the new section layout to landscape')
47+
def when_I_change_the_new_section_layout_to_landscape(context):
48+
new_height, new_width = context.original_dimensions
49+
section = context.section
50+
section.orientation = WD_ORIENT.LANDSCAPE
51+
section.page_width = new_width
52+
section.page_height = new_height
53+
54+
3155
# then ====================================================
3256

3357
@then('I can access a section by index')
@@ -55,9 +79,32 @@ def then_I_can_iterate_over_the_sections(context):
5579
assert actual_count == 3
5680

5781

82+
@then('the document has two sections')
83+
def then_the_document_has_two_sections(context):
84+
assert len(context.document.sections) == 2
85+
86+
87+
@then('the first section is portrait')
88+
def then_the_first_section_is_portrait(context):
89+
first_section = context.document.sections[0]
90+
expected_width, expected_height = context.original_dimensions
91+
assert first_section.orientation == WD_ORIENT.PORTRAIT
92+
assert first_section.page_width == expected_width
93+
assert first_section.page_height == expected_height
94+
95+
5896
@then('the length of the section collection is 3')
5997
def then_the_length_of_the_section_collection_is_3(context):
6098
sections = context.document.sections
6199
assert len(sections) == 3, (
62100
'expected len(sections) of 2, got %s' % len(sections)
63101
)
102+
103+
104+
@then('the second section is landscape')
105+
def then_the_second_section_is_landscape(context):
106+
new_section = context.document.sections[-1]
107+
expected_height, expected_width = context.original_dimensions
108+
assert new_section.orientation == WD_ORIENT.LANDSCAPE
109+
assert new_section.page_width == expected_width
110+
assert new_section.page_height == expected_height
20.8 KB
Binary file not shown.

0 commit comments

Comments
 (0)
X Tutup