X Tutup
Skip to content

Commit d5073ee

Browse files
author
Steve Canny
committed
sect: add Section.page_width setter
1 parent 100f07b commit d5073ee

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

docx/oxml/section.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ def page_width(self):
5757
return None
5858
return pgSz.w
5959

60+
@page_width.setter
61+
def page_width(self, value):
62+
pgSz = self.get_or_add_pgSz()
63+
pgSz.w = value
64+
6065
@property
6166
def start_type(self):
6267
"""

docx/section.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ def page_width(self):
3535
"""
3636
return self._sectPr.page_width
3737

38+
@page_width.setter
39+
def page_width(self, value):
40+
self._sectPr.page_width = value
41+
3842
@property
3943
def start_type(self):
4044
"""

tests/test_section.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ def it_knows_its_page_width(self, page_width_get_fixture):
3030
section, expected_page_width = page_width_get_fixture
3131
assert section.page_width == expected_page_width
3232

33+
def it_can_change_its_page_width(self, page_width_set_fixture):
34+
section, new_page_width, expected_xml = page_width_set_fixture
35+
section.page_width = new_page_width
36+
assert section._sectPr.xml == expected_xml
37+
3338
def it_knows_its_page_height(self, page_height_get_fixture):
3439
section, expected_page_height = page_height_get_fixture
3540
assert section.page_height == expected_page_height
@@ -60,6 +65,20 @@ def page_width_get_fixture(self, request):
6065
section = Section(sectPr)
6166
return section, expected_page_width
6267

68+
@pytest.fixture(params=[
69+
(None, None),
70+
(Inches(1), 1440),
71+
])
72+
def page_width_set_fixture(self, request):
73+
new_page_width, expected_w_val = request.param
74+
# section ----------------------
75+
sectPr = self.sectPr_bldr().element
76+
section = Section(sectPr)
77+
# expected_xml -----------------
78+
pgSz_bldr = self.pgSz_bldr(w=expected_w_val)
79+
expected_xml = self.sectPr_bldr(pgSz_bldr).xml()
80+
return section, new_page_width, expected_xml
81+
6382
@pytest.fixture(params=[
6483
(False, None, WD_SECTION.NEW_PAGE),
6584
(True, None, WD_SECTION.NEW_PAGE),
@@ -98,7 +117,7 @@ def start_type_set_fixture(self, request):
98117

99118
# fixture components ---------------------------------------------
100119

101-
def pgSz_bldr(self, has_pgSz, w=None, h=None):
120+
def pgSz_bldr(self, has_pgSz=True, w=None, h=None):
102121
if not has_pgSz:
103122
return None
104123
pgSz_bldr = a_pgSz()

0 commit comments

Comments
 (0)
X Tutup