Skip to content

Commit 6fbda35

Browse files
author
Steve Canny
committed
set: add SettingsPart.default()
1 parent f60fddd commit 6fbda35

File tree

4 files changed

+58
-2
lines changed

4 files changed

+58
-2
lines changed

docx/parts/settings.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@
88
absolute_import, division, print_function, unicode_literals
99
)
1010

11+
import os
12+
13+
from ..opc.constants import CONTENT_TYPE as CT
14+
from ..opc.packuri import PackURI
1115
from ..opc.part import XmlPart
16+
from ..oxml import parse_xml
1217
from ..settings import Settings
1318

1419

@@ -22,7 +27,10 @@ def default(cls, package):
2227
Return a newly created settings part, containing a default
2328
`w:settings` element tree.
2429
"""
25-
raise NotImplementedError
30+
partname = PackURI('/word/settings.xml')
31+
content_type = CT.WML_SETTINGS
32+
element = parse_xml(cls._default_settings_xml())
33+
return cls(partname, content_type, element, package)
2634

2735
@property
2836
def settings(self):
@@ -31,3 +39,16 @@ def settings(self):
3139
containing the document-level settings for this document.
3240
"""
3341
return Settings(self.element)
42+
43+
@classmethod
44+
def _default_settings_xml(cls):
45+
"""
46+
Return a bytestream containing XML for a default settings part.
47+
"""
48+
path = os.path.join(
49+
os.path.split(__file__)[0], '..', 'templates',
50+
'default-settings.xml'
51+
)
52+
with open(path, 'rb') as f:
53+
xml_bytes = f.read()
54+
return xml_bytes

docx/templates/default-settings.xml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?xml version='1.0' encoding='UTF-8' standalone='yes'?>
2+
<w:settings
3+
xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math"
4+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
5+
xmlns:o="urn:schemas-microsoft-com:office:office"
6+
xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"
7+
xmlns:sl="http://schemas.openxmlformats.org/schemaLibrary/2006/main"
8+
xmlns:v="urn:schemas-microsoft-com:vml"
9+
xmlns:w10="urn:schemas-microsoft-com:office:word"
10+
xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml"
11+
xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
12+
mc:Ignorable="w14"
13+
>
14+
<w:defaultTabStop w:val="720"/>
15+
<w:characterSpacingControl w:val="doNotCompress"/>
16+
<w:savePreviewPicture/>
17+
<w:compat>
18+
<w:useFELayout/>
19+
<w:compatSetting w:name="compatibilityMode" w:uri="http://schemas.microsoft.com/office/word" w:val="14"/>
20+
<w:compatSetting w:name="overrideTableStyleFontSizeAndJustification" w:uri="http://schemas.microsoft.com/office/word" w:val="1"/>
21+
<w:compatSetting w:name="enableOpenTypeFeatures" w:uri="http://schemas.microsoft.com/office/word" w:val="1"/>
22+
<w:compatSetting w:name="doNotFlipMirrorIndents" w:uri="http://schemas.microsoft.com/office/word" w:val="1"/>
23+
</w:compat>
24+
<w:clrSchemeMapping w:bg1="light1" w:t1="dark1" w:bg2="light2" w:t2="dark2" w:accent1="accent1" w:accent2="accent2" w:accent3="accent3" w:accent4="accent4" w:accent5="accent5" w:accent6="accent6" w:hyperlink="hyperlink" w:followedHyperlink="followedHyperlink"/>
25+
<w:doNotAutoCompressPictures/>
26+
</w:settings>

features/doc-settings.feature

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ Feature: Access to document settings
44
I access to settings stored in the settings part
55

66

7-
@wip
87
Scenario Outline: Access document settings
98
Given a document having <a-or-no> settings part
109
Then document.settings is a Settings object

tests/parts/test_settings.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import pytest
1212

1313
from docx.opc.constants import CONTENT_TYPE as CT
14+
from docx.opc.package import OpcPackage
1415
from docx.opc.part import PartFactory
1516
from docx.package import Package
1617
from docx.parts.settings import SettingsPart
@@ -36,6 +37,15 @@ def it_provides_access_to_its_settings(self, settings_fixture):
3637
Settings_.assert_called_once_with(settings_part.element)
3738
assert settings is settings_
3839

40+
def it_constructs_a_default_settings_part_to_help(self):
41+
package = OpcPackage()
42+
settings_part = SettingsPart.default(package)
43+
assert isinstance(settings_part, SettingsPart)
44+
assert settings_part.partname == '/word/settings.xml'
45+
assert settings_part.content_type == CT.WML_SETTINGS
46+
assert settings_part.package is package
47+
assert len(settings_part.element) == 6
48+
3949
# fixtures -------------------------------------------------------
4050

4151
@pytest.fixture

0 commit comments

Comments
 (0)