用另一个变量替换句子的部分



我正在尝试将image_id变量与保存在name in zip_file.namelist()下的另一个字符串的部分进行迭代匹配。如果image_id变量与name字符串中的(d+)部分不匹配,则数字(d+)将替换为image_id变量。然而,我无法做到这一点。请查看下面我的当前代码:

this_folder = dirname("C:/Users/xxxxx/Documents/xxxx/")
doc_path = join(this_folder, 'Sample - Copy v1.docx')    
doc = docx.Document(doc_path)
def iter_block_items(parent):
if isinstance(parent, Document):
parent_elm = parent.element.body
elif isinstance(parent, _Cell):
parent_elm = parent._tc
elif isinstance(parent, _Row):
parent_elm = parent._tr
else:
raise ValueError("something's not right")
for child in parent_elm.iterchildren():
if isinstance(child, CT_P):
yield Paragraph(child, parent)
elif isinstance(child, CT_Tbl):
yield Table(child, parent)
found_images = []
for block in iter_block_items(doc):
if isinstance(block, Paragraph):
# Read a paragraph.
if block.text != "":
print(block.text)
Sheet1.write(index_row, 0, block.text)
index_row += 1
# Indicate if an image is found in the paragraph.
if 'graphicData' in block._p.xml:
xml = str(block._p.xml)
index_column = 0
for image_id in re.findall(r'<wp:docPr id="(d+)"', XML):
#intending to paste to another Excel workbook later on:
found_images.append((index_row, index_column, image_id))
index_column += 1
image_paragraphs.append(block)
index_row += 1
found_images   
with ZipFile(doc_path) as zip_file:
zipped_images = {}

#sourcing for images in the document (.docx):
for name in zip_file.namelist():
if name.startswith('word/media/'):
m = re.fullmatch('word/media/image(d+).w+', name)
if m:
zipped_images[m.group(0)] = name   
#print(name) 
#output: word/media/image1.jpeg

for index_row, index_column, image_id in found_images:
#print(image_id)
#output: 18
if image_id not in name:
regex = r"(?<=word/media/image)d+(?=.w+$)"
name = re.sub(regex, image_id, name)
#print(name)
#output: customXml/_rels/item4.xml.rels
#intended output: word/media/image18.jpeg
#Error occurs here:
with zip_file.open(zipped_images[image_id]) as image_file:
#output: KeyError: '18'
image_data = BytesIO(image_file.read())
Sheet1.insert_image(index_row, index_column, 'image%s' % image_id, {'image_data': image_data})

有人能告诉我哪里出了问题吗?非常感谢。

像这样使用re.sub

if image_id not in name:
re.sub('d', image_id, name)

最新更新