Skip to content

Cannot merge documents with image #38

Closed

Description

@cvas22

Copied from python-docx-template issue 383, opened by @simjan on 2021-09-21.


Describe the bug

cannot import a document (with image) within a master doc that has also image

To Reproduce

sandbox.zip

Expected behavior

the generated docx file (result.docx) has to contains the "master" image (from master template) and the "SUB" image (from sub template file)

Screenshots

current:
image
expected:
image

Additional context

I think the problem is located in the contained images references... in both themplate, the image is referenced as image1. In the final document, both references point to image1 but in theory, one is from master and the other comes from sub template...
I was not able to find a way to overwritte sub template reference with a "sub" prefix in order to keep correct references...


Comments from the original thread

@kaiserls on 2021-10-25:

In docxcompose/composer.py#23 you find the constant variable
REFERENCED_PARTS_IGNORED_RELTYPES = set([ RT.IMAGE, RT.HEADER, RT.FOOTER, ])
I commented out RT.IMAGE and now the inclusion of images is working for the examples which I tried. Also your sandbox example is functional with this fix.

@simjan on 2021-10-26:

@kaiserls Thanks for the reply, but I don't use docxcompose and the lib seems not to be used by docxtpl or python-docx...

I tried to add the lib and to import it but without any result...

import os

from docxtpl import DocxTemplate

##
# https://github.com/elapouya/python-docx-template/issues/383
from docxcompose.composer import REFERENCED_PARTS_IGNORED_RELTYPES
from docx.opc.constants import RELATIONSHIP_TYPE as RT
REFERENCED_PARTS_IGNORED_RELTYPES.remove(RT.IMAGE)
##

tpl = DocxTemplate(os.path.join(os.path.dirname(__file__), "templates", 'master_tpl.docx'))
sd = tpl.new_subdoc(os.path.join(os.path.dirname(__file__), "templates", 'subdoc.docx'))

tpl.render({'mysubdoc': sd})
tpl.save(os.path.join(os.path.dirname(__file__), 'result.docx'))

@kaiserls on 2021-11-12:

Hey,
docxtpl/template.py->DocxTemplate imports & uses
docxtpl/subdoc.py-> Subdoc ("from .subdoc import Subdoc") imports & uses
docxcompose/composer.py->Composer ("from docxcompose.composer import Composer") has
REFERENCED_PARTS_IGNORED_RELTYPES

However I can't reproduce the behaviour you describe at the moment. Even using google colab and your sandbox.

!pip install docxtpl
import os

from docxtpl import DocxTemplate

tpl = DocxTemplate('master_tpl.docx')
sd = tpl.new_subdoc('subdoc.docx')

tpl.render({'mysubdoc': sd})
tpl.save('./result.docx')

Maybe you simply have to upgrade/reinstall the docxtpl package and the packages used by it.
pip install --upgrade --force-reinstall docxtpl

Activity

  1. added
    bug
    on Nov 9, 2026
  2. cvas22 commented on Nov 22, 2026

    @cvas22
    OwnerAuthor

    @simjan @kaiserls this works on current main, a master with its own image and a subdoc with a different image both keep their images (the fix in #172 covers the related case). Closing, reopen with your files if it still happens.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions