Python reads pictures from word and converts the pictures into links

created at 07-28-2021 views: 2

Read the picture and convert the picture into a link

from docx import Document
from os.path import basename
import re


def upload_image(image_data):
    image_url = "image link"
    return image_url


file_name = "/Users/targetfile.docx"
doc = Document(file_name)
a = list()
pattern = re.compile('rId\d+')
for graph in doc.paragraphs:
    b = list()
    for run in graph.runs:
        if run.text != '':
            b.append(run.text)
        else:
            # b.append(pattern.search(run.element.xml))
            content_id = pattern.search(run.element.xml).group(0)
            try:
                content_type = doc.part.related_parts[content_id].content_type
            except KeyError as e:
                print(e)
                continue
            if not content_type.startswith('image'):
                continue
            img_name = basename(doc.part.related_parts[content_id].partname)
            img_data = doc.part.related_parts[content_id].blob
            b.append(upload_image(img_data))
    b_str=f"{''.join(b)}"
    a.append(b_str)
print(a)
created at:07-28-2021
edited at: 07-28-2021: