用Python从word文档中提取图像

2024-06-26 04:20:38 发布

您现在位置:Python中文网/ 问答频道 /正文

如何使用python从word文档中提取图像/徽标并将其存储在文件夹中。下面的代码将docx转换为html,但是它没有从html中提取图像。任何建议都会有很大的帮助。在

    profile_path = <file path>
    result=mammoth.convert_to_html( profile_path)
    f = open(profile_path, 'rb')
    b = open(profile_html, 'wb')
    document = mammoth.convert_to_html(f)
    b.write(document.value.encode('utf8'))
    f.close()
    b.close()

Tags: topath代码文档图像文件夹convertclose
1条回答
网友
1楼 · 发布于 2024-06-26 04:20:38

您可以使用docx2txt库,它将读取.docx文档并将图像导出到指定的目录(必须存在)。在

!pip install docx2txt
import docx2txt
text = docx2txt.process("/path/your_word_doc.docx", '/home/example/img/') 

执行后,图像将位于/home/example/img/中,变量text将包含文档文本。它们将被命名为image1.png。。。图像.png按照外表的顺序。在

注意:Word文档必须是.docx格式。在

相关问题 更多 >