Skip to content

Commit a6fde18

Browse files
committed
acpt: add Document.start_bookmark() scenario
1 parent c58ac35 commit a6fde18

3 files changed

Lines changed: 86 additions & 60 deletions

File tree

features/doc-document.feature

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Feature: Document properties and methods
2+
In order manipulate a Word document
3+
As a developer using python-docx
4+
I need properties and methods on the Document object
5+
6+
@wip
7+
Scenario: Document.start_bookmark()
8+
Given a Document object as document
9+
When I assign bookmark = document.start_bookmark("Target")
10+
Then bookmark.name == "Target"
11+
And bookmark.id is an int

features/steps/bookmarks.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,18 @@ def given_a_Bookmarks_object_of_length_5_as_bookmarks(context):
2323
# then =====================================================
2424

2525

26+
@then("bookmark.id is an int")
27+
def then_bookmark_id_is_an_int(context):
28+
bookmark = context.bookmark
29+
assert isinstance(bookmark.id, int)
30+
31+
32+
@then('bookmark.name == "Target"')
33+
def then_bookmark_name_eq_Target(context):
34+
bookmark = context.bookmark
35+
assert bookmark.name == "Target"
36+
37+
2638
@then('bookmarks.get({name}) returns bookmark named "{name}" with id {id}')
2739
def then_bookmark_get_returns_bookmark_object(context, name, id):
2840
bookmark = context.bookmarks.get(name)

features/steps/document.py

Lines changed: 63 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -22,44 +22,45 @@
2222

2323
# given ===================================================
2424

25-
@given('a blank document')
25+
26+
@given("a blank document")
2627
def given_a_blank_document(context):
27-
context.document = Document(test_docx('doc-word-default-blank'))
28+
context.document = Document(test_docx("doc-word-default-blank"))
2829

2930

30-
@given('a Document object as document')
31+
@given("a Document object as document")
3132
def given_a_Document_object_as_document(context):
32-
context.document = Document(test_docx('doc-default'))
33+
context.document = Document(test_docx("doc-default"))
3334

3435

35-
@given('a document having built-in styles')
36+
@given("a document having built-in styles")
3637
def given_a_document_having_builtin_styles(context):
3738
context.document = Document()
3839

3940

40-
@given('a document having inline shapes')
41+
@given("a document having inline shapes")
4142
def given_a_document_having_inline_shapes(context):
42-
context.document = Document(test_docx('shp-inline-shape-access'))
43+
context.document = Document(test_docx("shp-inline-shape-access"))
4344

4445

45-
@given('a document having sections')
46+
@given("a document having sections")
4647
def given_a_document_having_sections(context):
47-
context.document = Document(test_docx('doc-access-sections'))
48+
context.document = Document(test_docx("doc-access-sections"))
4849

4950

50-
@given('a document having styles')
51+
@given("a document having styles")
5152
def given_a_document_having_styles(context):
52-
context.document = Document(test_docx('sty-having-styles-part'))
53+
context.document = Document(test_docx("sty-having-styles-part"))
5354

5455

55-
@given('a document having three tables')
56+
@given("a document having three tables")
5657
def given_a_document_having_three_tables(context):
57-
context.document = Document(test_docx('tbl-having-tables'))
58+
context.document = Document(test_docx("tbl-having-tables"))
5859

5960

60-
@given('a single-section document having portrait layout')
61+
@given("a single-section document having portrait layout")
6162
def given_a_single_section_document_having_portrait_layout(context):
62-
context.document = Document(test_docx('doc-add-section'))
63+
context.document = Document(test_docx("doc-add-section"))
6364
section = context.document.sections[-1]
6465
context.original_dimensions = (section.page_width, section.page_height)
6566

@@ -71,55 +72,53 @@ def given_a_single_section_Document_object_with_headers_and_footers(context):
7172

7273
# when ====================================================
7374

74-
@when('I add a 2 x 2 table specifying only row and column count')
75+
76+
@when("I add a 2 x 2 table specifying only row and column count")
7577
def when_add_2x2_table_specifying_only_row_and_col_count(context):
7678
document = context.document
7779
document.add_table(rows=2, cols=2)
7880

7981

80-
@when('I add a 2 x 2 table specifying style \'{style_name}\'')
82+
@when("I add a 2 x 2 table specifying style '{style_name}'")
8183
def when_add_2x2_table_specifying_style_name(context, style_name):
8284
document = context.document
8385
document.add_table(rows=2, cols=2, style=style_name)
8486

8587

86-
@when('I add a heading specifying level={level}')
88+
@when("I add a heading specifying level={level}")
8789
def when_add_heading_specifying_level(context, level):
8890
context.document.add_heading(level=int(level))
8991

9092

91-
@when('I add a heading specifying only its text')
93+
@when("I add a heading specifying only its text")
9294
def when_add_heading_specifying_only_its_text(context):
9395
document = context.document
94-
context.heading_text = text = 'Spam vs. Eggs'
96+
context.heading_text = text = "Spam vs. Eggs"
9597
document.add_heading(text)
9698

9799

98-
@when('I add a page break to the document')
100+
@when("I add a page break to the document")
99101
def when_add_page_break_to_document(context):
100102
document = context.document
101103
document.add_page_break()
102104

103105

104-
@when('I add a paragraph specifying its style as a {kind}')
106+
@when("I add a paragraph specifying its style as a {kind}")
105107
def when_I_add_a_paragraph_specifying_its_style_as_a(context, kind):
106108
document = context.document
107-
style = context.style = document.styles['Heading 1']
108-
style_spec = {
109-
'style object': style,
110-
'style name': 'Heading 1',
111-
}[kind]
109+
style = context.style = document.styles["Heading 1"]
110+
style_spec = {"style object": style, "style name": "Heading 1"}[kind]
112111
document.add_paragraph(style=style_spec)
113112

114113

115-
@when('I add a paragraph specifying its text')
114+
@when("I add a paragraph specifying its text")
116115
def when_add_paragraph_specifying_text(context):
117116
document = context.document
118-
context.paragraph_text = 'foobar'
117+
context.paragraph_text = "foobar"
119118
document.add_paragraph(context.paragraph_text)
120119

121120

122-
@when('I add a paragraph without specifying text or style')
121+
@when("I add a paragraph without specifying text or style")
123122
def when_add_paragraph_without_specifying_text_or_style(context):
124123
document = context.document
125124
document.add_paragraph()
@@ -129,39 +128,44 @@ def when_add_paragraph_without_specifying_text_or_style(context):
129128
def when_add_picture_specifying_width_and_height(context):
130129
document = context.document
131130
context.picture = document.add_picture(
132-
test_file('monty-truth.png'),
133-
width=Inches(1.75), height=Inches(2.5)
131+
test_file("monty-truth.png"), width=Inches(1.75), height=Inches(2.5)
134132
)
135133

136134

137-
@when('I add a picture specifying a height of 1.5 inches')
135+
@when("I add a picture specifying a height of 1.5 inches")
138136
def when_add_picture_specifying_height(context):
139137
document = context.document
140138
context.picture = document.add_picture(
141-
test_file('monty-truth.png'), height=Inches(1.5)
139+
test_file("monty-truth.png"), height=Inches(1.5)
142140
)
143141

144142

145-
@when('I add a picture specifying a width of 1.5 inches')
143+
@when("I add a picture specifying a width of 1.5 inches")
146144
def when_add_picture_specifying_width(context):
147145
document = context.document
148146
context.picture = document.add_picture(
149-
test_file('monty-truth.png'), width=Inches(1.5)
147+
test_file("monty-truth.png"), width=Inches(1.5)
150148
)
151149

152150

153-
@when('I add a picture specifying only the image file')
151+
@when("I add a picture specifying only the image file")
154152
def when_add_picture_specifying_only_image_file(context):
155153
document = context.document
156-
context.picture = document.add_picture(test_file('monty-truth.png'))
154+
context.picture = document.add_picture(test_file("monty-truth.png"))
157155

158156

159-
@when('I add an even-page section to the document')
157+
@when("I add an even-page section to the document")
160158
def when_I_add_an_even_page_section_to_the_document(context):
161159
context.section = context.document.add_section(WD_SECTION.EVEN_PAGE)
162160

163161

164-
@when('I change the new section layout to landscape')
162+
@when('I assign bookmark = document.start_bookmark("Target")')
163+
def when_I_assign_bookmark_eq_document_start_bookmark(context):
164+
document = context.document
165+
context.bookmark = document.start_bookmark("Target")
166+
167+
168+
@when("I change the new section layout to landscape")
165169
def when_I_change_the_new_section_layout_to_landscape(context):
166170
new_height, new_width = context.original_dimensions
167171
section = context.section
@@ -177,21 +181,22 @@ def when_I_execute_section_eq_document_add_section(context):
177181

178182
# then ====================================================
179183

180-
@then('document.bookmarks is a Bookmarks object')
184+
185+
@then("document.bookmarks is a Bookmarks object")
181186
def then_document_bookmarks_is_a_Bookmarks_object(context):
182187
actual = context.document.bookmarks.__class__.__name__
183-
expected = 'Bookmarks'
184-
assert actual == expected, 'document.bookmarks is a %s object' % actual
188+
expected = "Bookmarks"
189+
assert actual == expected, "document.bookmarks is a %s object" % actual
185190

186191

187-
@then('document.inline_shapes is an InlineShapes object')
192+
@then("document.inline_shapes is an InlineShapes object")
188193
def then_document_inline_shapes_is_an_InlineShapes_object(context):
189194
document = context.document
190195
inline_shapes = document.inline_shapes
191196
assert isinstance(inline_shapes, InlineShapes)
192197

193198

194-
@then('document.paragraphs is a list containing three paragraphs')
199+
@then("document.paragraphs is a list containing three paragraphs")
195200
def then_document_paragraphs_is_a_list_containing_three_paragraphs(context):
196201
document = context.document
197202
paragraphs = document.paragraphs
@@ -201,20 +206,20 @@ def then_document_paragraphs_is_a_list_containing_three_paragraphs(context):
201206
assert isinstance(paragraph, Paragraph)
202207

203208

204-
@then('document.sections is a Sections object')
209+
@then("document.sections is a Sections object")
205210
def then_document_sections_is_a_Sections_object(context):
206211
sections = context.document.sections
207-
msg = 'document.sections not instance of Sections'
212+
msg = "document.sections not instance of Sections"
208213
assert isinstance(sections, Sections), msg
209214

210215

211-
@then('document.styles is a Styles object')
216+
@then("document.styles is a Styles object")
212217
def then_document_styles_is_a_Styles_object(context):
213218
styles = context.document.styles
214219
assert isinstance(styles, Styles)
215220

216221

217-
@then('document.tables is a list containing three tables')
222+
@then("document.tables is a list containing three tables")
218223
def then_document_tables_is_a_list_containing_three_tables(context):
219224
document = context.document
220225
tables = document.tables
@@ -224,7 +229,7 @@ def then_document_tables_is_a_list_containing_three_tables(context):
224229
assert isinstance(table, Table)
225230

226231

227-
@then('the document contains a 2 x 2 table')
232+
@then("the document contains a 2 x 2 table")
228233
def then_the_document_contains_a_2x2_table(context):
229234
table = context.document.tables[-1]
230235
assert isinstance(table, Table)
@@ -233,12 +238,12 @@ def then_the_document_contains_a_2x2_table(context):
233238
context.table_ = table
234239

235240

236-
@then('the document has two sections')
241+
@then("the document has two sections")
237242
def then_the_document_has_two_sections(context):
238243
assert len(context.document.sections) == 2
239244

240245

241-
@then('the first section is portrait')
246+
@then("the first section is portrait")
242247
def then_the_first_section_is_portrait(context):
243248
first_section = context.document.sections[0]
244249
expected_width, expected_height = context.original_dimensions
@@ -247,24 +252,24 @@ def then_the_first_section_is_portrait(context):
247252
assert first_section.page_height == expected_height
248253

249254

250-
@then('the last paragraph contains only a page break')
255+
@then("the last paragraph contains only a page break")
251256
def then_last_paragraph_contains_only_a_page_break(context):
252257
document = context.document
253258
paragraph = document.paragraphs[-1]
254259
assert len(paragraph.runs) == 1
255260
assert len(paragraph.runs[0]._r) == 1
256-
assert paragraph.runs[0]._r[0].type == 'page'
261+
assert paragraph.runs[0]._r[0].type == "page"
257262

258263

259-
@then('the last paragraph contains the heading text')
264+
@then("the last paragraph contains the heading text")
260265
def then_last_p_contains_heading_text(context):
261266
document = context.document
262267
text = context.heading_text
263268
paragraph = document.paragraphs[-1]
264269
assert paragraph.text == text
265270

266271

267-
@then('the second section is landscape')
272+
@then("the second section is landscape")
268273
def then_the_second_section_is_landscape(context):
269274
new_section = context.document.sections[-1]
270275
expected_height, expected_width = context.original_dimensions
@@ -273,10 +278,8 @@ def then_the_second_section_is_landscape(context):
273278
assert new_section.page_height == expected_height
274279

275280

276-
@then('the style of the last paragraph is \'{style_name}\'')
281+
@then("the style of the last paragraph is '{style_name}'")
277282
def then_the_style_of_the_last_paragraph_is_style(context, style_name):
278283
document = context.document
279284
paragraph = document.paragraphs[-1]
280-
assert paragraph.style.name == style_name, (
281-
'got %s' % paragraph.style.name
282-
)
285+
assert paragraph.style.name == style_name, "got %s" % paragraph.style.name

0 commit comments

Comments
 (0)