|
12 | 12 | from docx.section import Section |
13 | 13 | from docx.shared import Inches |
14 | 14 |
|
15 | | -from .oxml.unitdata.section import a_pgMar, a_sectPr |
16 | 15 | from .unitutil.cxml import element, xml |
17 | 16 |
|
18 | 17 |
|
@@ -55,78 +54,58 @@ def it_can_change_its_orientation(self, orientation_set_fixture): |
55 | 54 | assert section._sectPr.xml == expected_xml |
56 | 55 |
|
57 | 56 | def it_knows_its_page_margins(self, margins_get_fixture): |
58 | | - section, left, right, top, bottom, gutter, header, footer = ( |
59 | | - margins_get_fixture |
60 | | - ) |
61 | | - assert section.left_margin == left |
62 | | - assert section.right_margin == right |
63 | | - assert section.top_margin == top |
64 | | - assert section.bottom_margin == bottom |
65 | | - assert section.gutter == gutter |
66 | | - assert section.header_distance == header |
67 | | - assert section.footer_distance == footer |
| 57 | + section, margin_prop_name, expected_value = margins_get_fixture |
| 58 | + value = getattr(section, margin_prop_name) |
| 59 | + assert value == expected_value |
68 | 60 |
|
69 | 61 | def it_can_change_its_page_margins(self, margins_set_fixture): |
70 | 62 | section, margin_prop_name, new_value, expected_xml = ( |
71 | 63 | margins_set_fixture |
72 | 64 | ) |
73 | | - print(section._sectPr.xml) |
74 | 65 | setattr(section, margin_prop_name, new_value) |
75 | | - print(section._sectPr.xml) |
76 | 66 | assert section._sectPr.xml == expected_xml |
77 | 67 |
|
78 | 68 | # fixtures ------------------------------------------------------- |
79 | 69 |
|
80 | 70 | @pytest.fixture(params=[ |
81 | | - (True, 720, 720, 720, 720, 720, 720, 720), |
82 | | - (True, None, 360, None, 360, None, 360, None), |
83 | | - (False, None, None, None, None, None, None, None), |
| 71 | + ('w:sectPr/w:pgMar{w:left=120}', 'left_margin', 76200), |
| 72 | + ('w:sectPr/w:pgMar{w:right=240}', 'right_margin', 152400), |
| 73 | + ('w:sectPr/w:pgMar{w:top=-360}', 'top_margin', -228600), |
| 74 | + ('w:sectPr/w:pgMar{w:bottom=480}', 'bottom_margin', 304800), |
| 75 | + ('w:sectPr/w:pgMar{w:gutter=600}', 'gutter', 381000), |
| 76 | + ('w:sectPr/w:pgMar{w:header=720}', 'header_distance', 457200), |
| 77 | + ('w:sectPr/w:pgMar{w:footer=840}', 'footer_distance', 533400), |
| 78 | + ('w:sectPr/w:pgMar', 'left_margin', None), |
| 79 | + ('w:sectPr', 'top_margin', None), |
84 | 80 | ]) |
85 | 81 | def margins_get_fixture(self, request): |
86 | | - (has_pgMar_child, left, right, top, bottom, gutter, header, |
87 | | - footer) = request.param |
88 | | - pgMar_bldr = self.pgMar_bldr(**{ |
89 | | - 'has_pgMar': has_pgMar_child, 'left': left, 'right': right, |
90 | | - 'top': top, 'bottom': bottom, 'gutter': gutter, 'header': header, |
91 | | - 'footer': footer |
92 | | - }) |
93 | | - sectPr = self.sectPr_bldr(pgMar_bldr).element |
94 | | - section = Section(sectPr) |
95 | | - expected_left = self.twips_to_emu(left) |
96 | | - expected_right = self.twips_to_emu(right) |
97 | | - expected_top = self.twips_to_emu(top) |
98 | | - expected_bottom = self.twips_to_emu(bottom) |
99 | | - expected_gutter = self.twips_to_emu(gutter) |
100 | | - expected_header = self.twips_to_emu(header) |
101 | | - expected_footer = self.twips_to_emu(footer) |
102 | | - return ( |
103 | | - section, expected_left, expected_right, expected_top, |
104 | | - expected_bottom, expected_gutter, expected_header, |
105 | | - expected_footer |
106 | | - ) |
| 82 | + sectPr_cxml, margin_prop_name, expected_value = request.param |
| 83 | + section = Section(element(sectPr_cxml)) |
| 84 | + return section, margin_prop_name, expected_value |
107 | 85 |
|
108 | 86 | @pytest.fixture(params=[ |
109 | | - ('left', 1440, 720), ('right', None, 1800), ('top', 2160, None), |
110 | | - ('bottom', 720, 2160), ('gutter', None, 360), ('header', 720, 630), |
111 | | - ('footer', None, 810) |
| 87 | + ('w:sectPr', 'left_margin', Inches(1), |
| 88 | + 'w:sectPr/w:pgMar{w:left=1440}'), |
| 89 | + ('w:sectPr', 'right_margin', Inches(0.5), |
| 90 | + 'w:sectPr/w:pgMar{w:right=720}'), |
| 91 | + ('w:sectPr', 'top_margin', Inches(-0.25), |
| 92 | + 'w:sectPr/w:pgMar{w:top=-360}'), |
| 93 | + ('w:sectPr', 'bottom_margin', Inches(0.75), |
| 94 | + 'w:sectPr/w:pgMar{w:bottom=1080}'), |
| 95 | + ('w:sectPr', 'gutter', Inches(0.25), |
| 96 | + 'w:sectPr/w:pgMar{w:gutter=360}'), |
| 97 | + ('w:sectPr', 'header_distance', Inches(1.25), |
| 98 | + 'w:sectPr/w:pgMar{w:header=1800}'), |
| 99 | + ('w:sectPr', 'footer_distance', Inches(1.35), |
| 100 | + 'w:sectPr/w:pgMar{w:footer=1944}'), |
| 101 | + ('w:sectPr', 'left_margin', None, 'w:sectPr/w:pgMar'), |
| 102 | + ('w:sectPr/w:pgMar{w:top=-360}', 'top_margin', Inches(0.6), |
| 103 | + 'w:sectPr/w:pgMar{w:top=864}'), |
112 | 104 | ]) |
113 | 105 | def margins_set_fixture(self, request): |
114 | | - margin_side, initial_margin, new_margin = request.param |
115 | | - # section ---------------------- |
116 | | - pgMar_bldr = self.pgMar_bldr(**{margin_side: initial_margin}) |
117 | | - sectPr = self.sectPr_bldr(pgMar_bldr).element |
118 | | - section = Section(sectPr) |
119 | | - # property name ---------------- |
120 | | - property_name = { |
121 | | - 'left': 'left_margin', 'right': 'right_margin', |
122 | | - 'top': 'top_margin', 'bottom': 'bottom_margin', |
123 | | - 'gutter': 'gutter', 'header': 'header_distance', |
124 | | - 'footer': 'footer_distance' |
125 | | - }[margin_side] |
126 | | - # expected_xml ----------------- |
127 | | - pgMar_bldr = self.pgMar_bldr(**{margin_side: new_margin}) |
128 | | - expected_xml = self.sectPr_bldr(pgMar_bldr).xml() |
129 | | - new_value = self.twips_to_emu(new_margin) |
| 106 | + sectPr_cxml, property_name, new_value, expected_cxml = request.param |
| 107 | + section = Section(element(sectPr_cxml)) |
| 108 | + expected_xml = xml(expected_cxml) |
130 | 109 | return section, property_name, new_value, expected_xml |
131 | 110 |
|
132 | 111 | @pytest.fixture(params=[ |
@@ -224,29 +203,3 @@ def start_type_set_fixture(self, request): |
224 | 203 | section = Section(element(initial_cxml)) |
225 | 204 | expected_xml = xml(expected_cxml) |
226 | 205 | return section, new_start_type, expected_xml |
227 | | - |
228 | | - # fixture components --------------------------------------------- |
229 | | - |
230 | | - @staticmethod |
231 | | - def twips_to_emu(twips): |
232 | | - if twips is None: |
233 | | - return None |
234 | | - return twips * 635 |
235 | | - |
236 | | - def pgMar_bldr(self, **kwargs): |
237 | | - if kwargs.pop('has_pgMar', True) is False: |
238 | | - return None |
239 | | - pgMar_bldr = a_pgMar() |
240 | | - for key, value in kwargs.items(): |
241 | | - if value is None: |
242 | | - continue |
243 | | - set_attr_method = getattr(pgMar_bldr, 'with_%s' % key) |
244 | | - set_attr_method(value) |
245 | | - return pgMar_bldr |
246 | | - |
247 | | - def sectPr_bldr(self, *child_bldrs): |
248 | | - sectPr_bldr = a_sectPr().with_nsdecls() |
249 | | - for child_bldr in child_bldrs: |
250 | | - if child_bldr is not None: |
251 | | - sectPr_bldr.with_child(child_bldr) |
252 | | - return sectPr_bldr |
0 commit comments