|
12 | 12 | from docx.section import Section |
13 | 13 | from docx.shared import Inches |
14 | 14 |
|
15 | | -from .oxml.unitdata.section import a_pgSz, a_sectPr, a_type |
| 15 | +from .oxml.unitdata.section import a_pgMar, a_pgSz, a_sectPr, a_type |
16 | 16 |
|
17 | 17 |
|
18 | 18 | class DescribeSection(object): |
@@ -52,8 +52,47 @@ def it_can_change_its_orientation(self, orientation_set_fixture): |
52 | 52 | section.orientation = new_orientation |
53 | 53 | assert section._sectPr.xml == expected_xml |
54 | 54 |
|
| 55 | + def it_knows_its_page_margins(self, margins_get_fixture): |
| 56 | + section, left, right, top, bottom, gutter, header, footer = ( |
| 57 | + margins_get_fixture |
| 58 | + ) |
| 59 | + assert section.left_margin == left |
| 60 | + assert section.right_margin == right |
| 61 | + assert section.top_margin == top |
| 62 | + assert section.bottom_margin == bottom |
| 63 | + assert section.gutter == gutter |
| 64 | + assert section.header_distance == header |
| 65 | + assert section.footer_distance == footer |
| 66 | + |
55 | 67 | # fixtures ------------------------------------------------------- |
56 | 68 |
|
| 69 | + @pytest.fixture(params=[ |
| 70 | + (True, 720, 720, 720, 720, 720, 720, 720), |
| 71 | + (True, None, 360, None, 360, None, 360, None), |
| 72 | + (False, None, None, None, None, None, None, None), |
| 73 | + ]) |
| 74 | + def margins_get_fixture(self, request): |
| 75 | + (has_pgMar_child, left, right, top, bottom, gutter, header, |
| 76 | + footer) = request.param |
| 77 | + pgMar_bldr = self.pgMar_bldr( |
| 78 | + has_pgMar_child, left=left, right=right, top=top, bottom=bottom, |
| 79 | + gutter=gutter, header=header, footer=footer |
| 80 | + ) |
| 81 | + sectPr = self.sectPr_bldr(pgMar_bldr).element |
| 82 | + section = Section(sectPr) |
| 83 | + expected_left = left * 635 if left else None |
| 84 | + expected_right = right * 635 if right else None |
| 85 | + expected_top = top * 635 if top else None |
| 86 | + expected_bottom = bottom * 635 if bottom else None |
| 87 | + expected_gutter = gutter * 635 if gutter else None |
| 88 | + expected_header = header * 635 if header else None |
| 89 | + expected_footer = footer * 635 if footer else None |
| 90 | + return ( |
| 91 | + section, expected_left, expected_right, expected_top, |
| 92 | + expected_bottom, expected_gutter, expected_header, |
| 93 | + expected_footer |
| 94 | + ) |
| 95 | + |
57 | 96 | @pytest.fixture(params=[ |
58 | 97 | (True, 'landscape', WD_ORIENT.LANDSCAPE), |
59 | 98 | (True, 'portrait', WD_ORIENT.PORTRAIT), |
@@ -172,6 +211,28 @@ def start_type_set_fixture(self, request): |
172 | 211 |
|
173 | 212 | # fixture components --------------------------------------------- |
174 | 213 |
|
| 214 | + def pgMar_bldr( |
| 215 | + self, has_pgMar=True, left=None, right=None, top=None, |
| 216 | + bottom=None, header=None, footer=None, gutter=None): |
| 217 | + if not has_pgMar: |
| 218 | + return None |
| 219 | + pgMar_bldr = a_pgMar() |
| 220 | + if left is not None: |
| 221 | + pgMar_bldr.with_left(left) |
| 222 | + if right is not None: |
| 223 | + pgMar_bldr.with_right(right) |
| 224 | + if top is not None: |
| 225 | + pgMar_bldr.with_top(top) |
| 226 | + if bottom is not None: |
| 227 | + pgMar_bldr.with_bottom(bottom) |
| 228 | + if header is not None: |
| 229 | + pgMar_bldr.with_header(header) |
| 230 | + if footer is not None: |
| 231 | + pgMar_bldr.with_footer(footer) |
| 232 | + if gutter is not None: |
| 233 | + pgMar_bldr.with_gutter(gutter) |
| 234 | + return pgMar_bldr |
| 235 | + |
175 | 236 | def pgSz_bldr(self, has_pgSz=True, w=None, h=None, orient=None): |
176 | 237 | if not has_pgSz: |
177 | 238 | return None |
|
0 commit comments