iulica / docx-mailmerge

Mail merge for Office Open XML (docx) files without the need for Microsoft Office Word.
MIT License
55 stars 7 forks source link

Added content type footnotes+xml so mail merge fields are supported #13

Closed 402Martin closed 8 months ago

402Martin commented 8 months ago
  1. Added a 1 line change on CONTENT_TYPES, that allows the replacing of a mail-merge field on the footnotes of a docx.

Description

  1. Added a 1 line change on CONTENT_TYPES, that allows the replacing of a mail-merge field on the footnotes of a docx. "application/vnd.openxmlformats-officedocument.wordprocessingml.footnotes+xml",

Motivation and Context

Thes change allows for the merge of fields inside footnotes.

How Has This Been Tested?

  1. Created a small testing script:

    from io import BytesIO
    from mailmerge import MailMerge
    
    def mergeDocumentToLocalFile(templatePath, outputPath, mergeFields):
      print(f"Using template - {templatePath}")
      with open(templatePath, "rb") as file:
          template = file.read()
      buffer = BytesIO(template)
      with MailMerge(buffer) as document:
          document.merge(**mergeFields)
          output_filelike = BytesIO()
          document.write(output_filelike)
          # need to reset the stream before writing to file
          output_filelike.seek(0)
          with open(outputPath, "wb") as output_file:
              output_file.write(output_filelike.read())
          print("File saved to", outputPath)
  2. Used multiple documents to test with and without footnotes.
  3. Run the script with multiple documents

Screenshots (if appropriate):

Types of changes

Checklist:

iulica commented 8 months ago

Thanks for the fix. I will add it as it is, but I would have liked a test file and test script in the tests/ folder so it could be tested.

iulica commented 8 months ago

well, did you test it ? I get 10 unittest errors. I reverted it as it will probably give errors to existing documents.

iulica commented 8 months ago

Post it as an issue with an example docx and I will try to fix it in a way that doesn't break other files.

402Martin commented 8 months ago

Sorry @iulica, just realized I had 2 clones of the repo and runned the tests on the incorrect directory, will work on the fix and reopen once they run correctly. and add some tests for this

Apologise for all the issues