X Tutup
Skip to content

Commit 2c73213

Browse files
author
Steve Canny
committed
acpt: add scenarios for style access
1 parent 1fff811 commit 2c73213

File tree

7 files changed

+103
-1
lines changed

7 files changed

+103
-1
lines changed

docx/styles/__init__.py

Whitespace-only changes.

docx/styles/style.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# encoding: utf-8
2+
3+
"""
4+
Style object hierarchy.
5+
"""
6+
7+
from __future__ import (
8+
absolute_import, division, print_function, unicode_literals
9+
)
10+
11+
from ..shared import ElementProxy
12+
13+
14+
class BaseStyle(ElementProxy):
15+
"""
16+
Base class for the various types of style object, paragraph, character,
17+
table, and numbering.
18+
"""
19+
20+
__slots__ = ()

docx/styles/styles.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# encoding: utf-8
2+
3+
"""
4+
Styles object, container for all objects in the styles part.
5+
"""
6+
7+
from __future__ import (
8+
absolute_import, division, print_function, unicode_literals
9+
)
10+
11+
12+
class Styles(object):
13+
"""
14+
A collection of |Style| objects defined in a document. Supports
15+
``len()``, iteration, and dictionary-style access by style id and style
16+
UI name.
17+
"""

features/doc-styles.feature

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
Feature: Access document styles
2+
In order to discover and manipulate document styles
3+
As a developer using python-docx
4+
I need a way to access document styles
5+
6+
7+
@wip
8+
Scenario Outline: Access document styles collection
9+
Given a document having <styles-state>
10+
Then I can access the document styles collection
11+
And len(styles) is <style-count>
12+
13+
Examples: having styles or not
14+
| styles-state | style-count |
15+
| a styles part | 6 |
16+
| no styles part | 4 |
17+
18+
19+
@wip
20+
Scenario: Access style in style collection
21+
Given a document having a styles part
22+
Then I can iterate over its styles
23+
And I can access a style by style id
24+
And I can access a style by its UI name

features/steps/styles.py

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
from behave import given, then, when
88

99
from docx import Document
10+
from docx.styles.styles import Styles
11+
from docx.styles.style import BaseStyle
1012

1113
from helpers import test_docx
1214

@@ -19,6 +21,12 @@ def given_a_document_having_a_styles_part(context):
1921
context.document = Document(docx_path)
2022

2123

24+
@given('a document having no styles part')
25+
def given_a_document_having_no_styles_part(context):
26+
docx_path = test_docx('sty-having-no-styles-part')
27+
context.document = Document(docx_path)
28+
29+
2230
# when ====================================================
2331

2432
@when('I get the styles part from the document')
@@ -29,7 +37,40 @@ def when_get_styles_part_from_document(context):
2937

3038
# then =====================================================
3139

40+
@then('I can access a style by its UI name')
41+
def then_I_can_access_a_style_by_its_UI_name(context):
42+
styles = context.document.styles
43+
style = styles['Default Paragraph Font']
44+
assert isinstance(style, BaseStyle)
45+
46+
47+
@then('I can access a style by style id')
48+
def then_I_can_access_a_style_by_style_id(context):
49+
styles = context.document.styles
50+
style = styles['DefaultParagraphFont']
51+
assert isinstance(style, BaseStyle)
52+
53+
54+
@then('I can access the document styles collection')
55+
def then_I_can_access_the_document_styles_collection(context):
56+
document = context.document
57+
styles = document.styles
58+
assert isinstance(styles, Styles)
59+
60+
61+
@then('I can iterate over its styles')
62+
def then_I_can_iterate_over_its_styles(context):
63+
styles = [s for s in context.document.styles]
64+
assert len(styles) > 0
65+
assert all(isinstance(s, BaseStyle) for s in styles)
66+
67+
68+
@then('len(styles) is {style_count_str}')
69+
def then_len_styles_is_style_count(context, style_count_str):
70+
assert len(context.document.styles) == int(style_count_str)
71+
72+
3273
@then('the styles part has the expected number of style definitions')
3374
def then_styles_part_has_expected_number_of_style_definitions(context):
3475
styles_part = context.styles_part
35-
assert len(styles_part.styles) == 4
76+
assert len(styles_part.styles) == 6
8.16 KB
Binary file not shown.
211 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)
X Tutup