我正在尝试使用python-docx
将wmf文件插入docx,这将产生以下回溯。
Traceback (most recent call last):
File "C:/Users/ADMIN/PycharmProjects/ppt-to-word/ppt_reader.py", line 79, in <module>
read_ppt(path, file)
File "C:/Users/ADMIN/PycharmProjects/ppt-to-word/ppt_reader.py", line 73, in read_ppt
write_docx(ppt_data, False)
File "C:/Users/ADMIN/PycharmProjects/ppt-to-word/ppt_reader.py", line 31, in write_docx
document.add_picture(slide_data.get('picture_location'), width=Inches(5.0))
File "C:\Python34\lib\site-packages\docx\document.py", line 72, in add_picture
return run.add_picture(image_path_or_stream, width, height)
File "C:\Python34\lib\site-packages\docx\text\run.py", line 62, in add_picture
inline = self.part.new_pic_inline(image_path_or_stream, width, height)
File "C:\Python34\lib\site-packages\docx\parts\story.py", line 56, in new_pic_inline
rId, image = self.get_or_add_image(image_descriptor)
File "C:\Python34\lib\site-packages\docx\parts\story.py", line 29, in get_or_add_image
image_part = self._package.get_or_add_image_part(image_descriptor)
File "C:\Python34\lib\site-packages\docx\package.py", line 31, in get_or_add_image_part
return self.image_parts.get_or_add_image_part(image_descriptor)
File "C:\Python34\lib\site-packages\docx\package.py", line 74, in get_or_add_image_part
image = Image.from_file(image_descriptor)
File "C:\Python34\lib\site-packages\docx\image\image.py", line 55, in from_file
return cls._from_stream(stream, blob, filename)
File "C:\Python34\lib\site-packages\docx\image\image.py", line 176, in _from_stream
image_header = _ImageHeaderFactory(stream)
File "C:\Python34\lib\site-packages\docx\image\image.py", line 199, in _ImageHeaderFactory
raise UnrecognizedImageError
docx.image.exceptions.UnrecognizedImageError
图像文件为.wmf
格式。
任何帮助或建议表示赞赏。
答案 0 :(得分:0)
python-docx
通过“识别”其独特的标头来标识图像文件的类型。这样,它可以将JPEG与PNG,TIFF等区分开。这比映射文件扩展名更可靠,并且比要求用户告诉您类型更方便。这是一种很常见的方法。
此错误表示python-docx
找不到可识别的标头。 Windows图元文件格式(WMF)可能会很棘手,专有规范中还有很多回旋余地,而且该领域的文件样本也有所不同。
要解决此问题,我建议您使用能够识别文件的文件读取文件(我将从Pillow开头),然后将其“转换”为相同或其他格式,以期更正该文件。标头。
首先,我将尝试读取它并将其另存为WMF(如果可以的话,还可以另存为EMF)。这可能足以解决问题。如果您必须先更改为中间格式,然后再返回,则可能会造成损失,但总比没有好。
ImageMagick可能是另一个不错的选择,因为它的覆盖范围可能比Pillow更好。