import os.path as osp
from docx import Document, ImagePart
from docx import Document
from PIL import Image
doc = Document("./1-清溪2.docx")
for p in doc.paragraphs:
images = p._element.xpath('.//pic:pic') # 获取所有图片
for image in images:
print(image)
for img_id in image.xpath('.//a:blip/@r:embed'): # 获取图片id
part = doc.part.related_parts[img_id] # 根据图片id获取对应的图片
if isinstance(part, ImagePart):
# 保存图片
with open(osp.basename(part.partname), "wb") as f:
f.write(part.blob)