[Python] Replacing string with images in docx

Discussão em 'Python' iniciado por Stack, Novembro 23, 2025.

  1. Stack

    Stack Membro Participativo

    I have been trying to replace certain image strings located in cells in a table in a docx file with the related images located in a folder. Let a compiled list of all images = Images.

    My 'best' attempt so far is to iterate through all table cells in the document and attempt this task, as seen in the code below.

    for Image in Images:
    for table in document.tables:
    for row in table.rows:
    for cell in row.cells:
    for paragraph in cell.paragraphs:
    if Image in paragraph.text:

    for p in enumerate(cell.paragraphs):
    img_paragraph = p[1]

    runner = paragraph.add_run(img_paragraph)
    runner.add_picture(f'{Image}.gif', width=Cm(1))

    paragraph.text = paragraph.text.replace(Image, "")


    Unfortunately, I am getting the error code "TypeError: 'Paragraph' object is not iterable", but every time I solve one error code, I find myself staring at another.

    Continue reading...

Compartilhe esta Página