【问题标题】:Extract images from word document using Python使用 Python 从 word 文档中提取图像
【发布时间】:2019-06-03 13:31:05
【问题描述】:
如何使用 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()
【问题讨论】:
标签:
python
python-3.x
python-2.7
【解决方案1】:
您可以使用 docx2txt 库,它会读取您的 .docx 文档并将图像导出到您指定的目录(必须存在)。
!pip install docx2txt
import docx2txt
text = docx2txt.process("/path/your_word_doc.docx", '/home/example/img/')
执行后,您将在 /home/example/img/ 中拥有图像,而变量 text 将拥有文档文本。它们将按出现顺序命名为 image1.png ... imageN.png。
注意:Word 文档必须为 .docx 格式。
【解决方案2】:
没有任何库的原生
从 docx(它是一个 zip 文件的变体)中提取源图像,而不会失真或转换。
脱壳到操作系统并运行
tar -m -xf DocxWithImages.docx word/media
您将在提取到该名称的文件夹中的 word media 文件夹中找到源图像 Jpeg、PNG WMF 或其他图像。这些是没有比例或裁剪的纯源嵌入。
您可能会惊讶于可见区域可能比 docx 本身中使用的任何裁剪版本更大,因此需要注意 Word 并不总是按预期裁剪图像(令人尴尬的编辑失败的根源)
【解决方案3】:
使用python提取docx文件中的所有图片
1。使用 docxtxt
import docx2txt
#extract text
text = docx2txt.process(r"filepath_of_docx")
#extract text and write images in Temporary Image directory
text = docx2txt.process(r"filepath_of_docx",r"Temporary_Image_Directory")
2。使用 aspose
import aspose.words as aw
# load the Word document
doc = aw.Document(r"filepath")
# retrieve all shapes
shapes = doc.get_child_nodes(aw.NodeType.SHAPE, True)
imageIndex = 0
# loop through shapes
for shape in shapes :
shape = shape.as_shape()
if (shape.has_image) :
# set image file's name
imageFileName = f"Image.ExportImages.{imageIndex}_{aw.FileFormatUtil.image_type_to_extension(shape.image_data.image_type)}"
# save image
shape.image_data.save(imageFileName)
imageIndex += 1