Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug with KeyError: None #100

Open
luguoyixiazi opened this issue Dec 22, 2023 · 1 comment
Open

bug with KeyError: None #100

luguoyixiazi opened this issue Dec 22, 2023 · 1 comment

Comments

@luguoyixiazi
Copy link

in Composer.insert(self, index, doc, remove_property_fields)
...
--> 213 img_part = doc.part.rels[rid].target_part
215 new_img_part = self.pkg.image_parts._get_by_sha1(img_part.sha1)
216 if new_img_part is None:

KeyError: None

this happened when I append a document that has some pic to Composer like this:
tp = Document(os.path.join(directory, file))
master.append(tp)

@KaHuier
Copy link

KaHuier commented Jan 8, 2024

from docxcompose.image import ImageWrapper
from docxcompose.utils import NS
from docx.opc.constants import RELATIONSHIP_TYPE as RT
def add_shapes(self, doc, element):
    shapes = xpath(element, './/v:shape/v:imagedata')
    for shape in shapes:
        rid = shape.get('{%s}id' % NS['r'])
        if rid is None:  # 这里会有时候取不到 导致报错 重写
            continue
        img_part = doc.part.rels[rid].target_part

        new_img_part = self.pkg.image_parts._get_by_sha1(img_part.sha1)
        if new_img_part is None:
            image = ImageWrapper(img_part)
            new_img_part = self.pkg.image_parts._add_image_part(image)

        new_rid = self.doc.part.relate_to(new_img_part, RT.IMAGE)
        shape.set('{%s}id' % NS['r'], new_rid)
Composer.add_shapes = add_shapes 

Add this code to the front of your code.
You can fix it temporarily with this method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants