Skip to content

Cannot parse a document with complex field with no end node  #98

@MrBlueMoo

Description

@MrBlueMoo

Issue:

Certain documents cannot be appended when using docxcompose due to the error:

docxcompose.properties.InvalidComplexField: Complex field without end node is not supported

These particular documents we have noticed this on were generated in Word from a template. Using Word to amend/re-save the document still retains the issue. I have stripped one back and attached it as an example here:
example.docx

The relevant code used is simply creating a blank document, then appending the source document into it:

source_doc = Document(file_path)
dest_document = Document()
dest_composer = Composer(dest_document)
dest_composer.append(source_doc)

And the full traceback is:

Traceback (most recent call last):
  File "venv\lib\site-packages\docxcompose\properties.py", line 428, in __init__
    super(ComplexField, self).__init__(field_node)
  File "venv\lib\site-packages\docxcompose\properties.py", line 345, in __init__
    self.name, self.date_format = self._parse_fieldname_and_format()
  File "venv\lib\site-packages\docxcompose\properties.py", line 377, in _parse_fieldname_and_format
    self._get_fieldname_string())
  File "venv\lib\site-packages\docxcompose\properties.py", line 435, in _get_fieldname_string
    separate_run = self.get_separate_run()
  File "venv\lib\site-packages\docxcompose\properties.py", line 471, in get_separate_run
    if not self.w_p.index(separate) < self.w_p.index(self.end_run):
  File "venv\lib\site-packages\docxcompose\properties.py", line 458, in end_run
    raise InvalidComplexField(msg)
docxcompose.properties.InvalidComplexField: Complex field without end node is not supported

As our intended use allows users to upload documents, we need to cater for any documents that Word considers valid.

Expected behaviour

The document should be appended, and the complex field without an end node should just be ignored.

Complex fields without end nodes Word still considers as valid, if we look at the OpenXml notes at: https://learn.microsoft.com/en-us/dotnet/api/documentformat.openxml.wordprocessing.fieldchar?view=openxml-2.8.1 it mentions:

Also, if a complex field is not closed before the end of a document story, then no field shall be generated and each individual run shall be processed as if the field characters did not exist (i.e. the contents of all field code run content shall not be displayed, and the field results shall be displayed as literal text).

Version:

  • docxcompose 1.4.0
  • python-docx 0.8.11

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions