使用Python从word文档中提取图像
如何使用python从word文档中提取图像/徽标并将其存储在文件夹中。下面的代码将docx转换为html,但它不会从html中提取图像。任何提示/建议都会大有帮助使用Python从word文档中提取图像,python,python-3.x,python-2.7,Python,Python 3.x,Python 2.7,如何使用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 =
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()
profile\u路径=
结果=猛犸象。将\u转换为\u html(配置文件\u路径)
f=打开(纵断面路径“rb”)
b=打开(配置文件“wb”)
document=mammoth.convert_to_html(f)
b、 写入(document.value.encode('utf8'))
f、 关闭()
b、 关闭()
您可以使用库,它将读取您的.docx文档并将图像导出到您指定的目录(必须存在)
执行后,图像将显示在/home/example/img/中,变量文本将显示文档文本。它们将被命名为image1.png。。。imageN.png按外观顺序排列
注意:Word文档必须是.docx格式。如果允许您转换Word文件,您可以尝试将其转换为pdf格式,然后尝试使用此处描述的方法之一获取图像:我不知道它是否能满足您的需要,但我认为值得一试。
!pip install docx2txt
import docx2txt
text = docx2txt.process("/path/your_word_doc.docx", '/home/example/img/')