python

python怎么提取word中的图片

小亿
96
2024-03-14 15:42:28
栏目: 编程语言

要提取Word文档中的图片,可以使用Python的docx库来实现。以下是一个简单的示例代码:

from docx import Document

doc = Document('your_word_document.docx')

for paragraph in doc.paragraphs:
    for run in paragraph.runs:
        for inline_shape in run.inline_shapes:
            image = inline_shape.image
            image_bytes = image.blob
            with open(f"image_{image.id}.png", "wb") as f:
                f.write(image_bytes)

在上面的代码中,我们首先导入Document类并打开要提取图片的Word文档。然后,我们遍历文档中的每个段落、运行和内联形状,检查是否存在图片。如果存在图片,则将其保存为PNG文件。

请注意,这只是一个简单的示例代码,实际情况可能更复杂,具体操作还取决于Word文档中图片的存储方式。希望这可以帮助到您。

0
看了该问题的人还看了