bug with KeyError: None
luguoyixiazi opened this issue · 1 comments
luguoyixiazi commented
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 commented
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.