diff options
| author | Michael Williamson <mike@zwobble.org> | 2017-11-23 19:54:53 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2017-11-23 19:54:53 +0000 |
| commit | c6e6a2b462d0bc5b7a5ac61d26c907e66cbcc946 (patch) | |
| tree | 0ba9641dea203413923a08b44a8e7f56a5ee99f4 /mammoth | |
| parent | d44b2df9238e007cfe7bf1becf11b30e1e97d284 (diff) | |
Parse paragraph indents
Diffstat (limited to 'mammoth')
| -rw-r--r-- | mammoth/documents.py | 28 | ||||
| -rw-r--r-- | mammoth/docx/body_xml.py | 10 |
2 files changed, 36 insertions, 2 deletions
diff --git a/mammoth/documents.py b/mammoth/documents.py index e4372ab..b00f1bb 100644 --- a/mammoth/documents.py +++ b/mammoth/documents.py @@ -21,6 +21,24 @@ class Paragraph(HasChildren): style_name = cobble.field() numbering = cobble.field() alignment = cobble.field() + indent = cobble.field() + + +@cobble.data +class ParagraphIndent(object): + start = cobble.field() + end = cobble.field() + first_line = cobble.field() + hanging = cobble.field() + + +@cobble.data +class Indent(object): + left = cobble.field() + right = cobble.field() + first_line = cobble.field() + hanging = cobble.field() + @cobble.data class Run(HasChildren): @@ -86,8 +104,14 @@ def document(children, notes=None, comments=None): comments = [] return Document(children, notes, comments=comments) -def paragraph(children, style_id=None, style_name=None, numbering=None, alignment=None): - return Paragraph(children, style_id, style_name, numbering, alignment=alignment) +def paragraph(children, style_id=None, style_name=None, numbering=None, alignment=None, indent=None): + if indent is None: + indent = paragraph_indent() + + return Paragraph(children, style_id, style_name, numbering, alignment=alignment, indent=indent) + +def paragraph_indent(start=None, end=None, first_line=None, hanging=None): + return ParagraphIndent(start=start, end=end, first_line=first_line, hanging=hanging) def run( children, diff --git a/mammoth/docx/body_xml.py b/mammoth/docx/body_xml.py index 01920f0..cfe3493 100644 --- a/mammoth/docx/body_xml.py +++ b/mammoth/docx/body_xml.py @@ -120,6 +120,7 @@ def _create_reader(numbering, content_types, relationships, styles, docx_file, f properties = element.find_child_or_null("w:pPr") alignment = properties.find_child_or_null("w:jc").attributes.get("w:val") numbering = _read_numbering_properties(properties.find_child_or_null("w:numPr")) + indent = _read_paragraph_indent(properties.find_child_or_null("w:ind")) return _ReadResult.map_results( _read_paragraph_style(properties), @@ -130,6 +131,7 @@ def _create_reader(numbering, content_types, relationships, styles, docx_file, f style_name=style[1], numbering=numbering, alignment=alignment, + indent=indent, )).append_extra() def _read_paragraph_style(properties): @@ -203,6 +205,14 @@ def _create_reader(numbering, content_types, relationships, styles, docx_file, f else: return numbering.find_level(num_id, level_index) + def _read_paragraph_indent(element): + attributes = element.attributes + return documents.paragraph_indent( + start=attributes.get("w:start") or attributes.get("w:left"), + end=attributes.get("w:end") or attributes.get("w:right"), + first_line=attributes.get("w:firstLine"), + hanging=attributes.get("w:hanging"), + ) def tab(element): return _success(documents.tab()) |
