UnrecognizedImageError - 图像插入错误 - python-docx

2024-01-04

我正在尝试使用将 wmf 文件插入 docxpython-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 format.

任何帮助或建议表示赞赏。


python-docx通过“识别”其独特的标头来识别图像文件的类型。通过这种方式,它可以区分 JPEG 和 PNG、TIFF 等。这比映射文件扩展名更可靠,也比要求用户告诉您类型更方便。这是一种非常常见的方法。

该错误表明python-docx没有找到它识别的标头。 Windows 图元文件格式 (WMF) 在这种情况下可能会很棘手,专有规范和现场文件样本的变化有很大的余地。

要解决此问题,我建议您阅读包含以下内容的文件does识别它(我将从 Pillow 开始)并将其“转换”为相同或另一种格式,希望在此过程中纠正标头。

首先,我会尝试读取它并将其另存为 WMF(或者可能是 EMF,如果可以的话)。这可能足以达到目的。如果您必须更改为中间格式然后再返回,这可能会造成损失,但也许总比没有好。

ImageMagick 可能是另一个不错的选择,因为它的覆盖范围可能比 Pillow 更好。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

UnrecognizedImageError - 图像插入错误 - python-docx 的相关文章

  • 如何编写嵌套的 __init__.py 文件

    我正在努力解决嵌套问题 init py在我正在编写的Python包中 该包具有以下架构 module init py submodule1 init py source py submodule2 init py source py sub
  • python sys.path 故障排除

    python 文档位于http docs python org library sys html http docs python org library sys html比如说sys path is 从环境变量 PYTHONPATH 以及
  • 如何避免使用 python 处理空的标准输入?

    The sys stdin readline 返回之前等待 EOF 或新行 所以如果我有控制台输入 readline 等待用户输入 相反 我想打印帮助并在没有需要处理的情况下退出并显示错误 而不是等待用户输入 原因 我正在寻找一个Pytho
  • 多处理中的动态池大小?

    有没有办法动态调整multiprocessing Pool尺寸 我正在编写一个简单的服务器进程 它会产生工作人员来处理新任务 使用multiprocessing Process对于这种情况可能更适合 因为工作人员的数量不应该是固定的 但我需
  • 如何返回 cost, grad 作为 scipy 的 fmin_cg 函数的元组

    我怎样才能使 scipy 的fmin cg使用一个返回的函数cost and gradient作为元组 问题是有f对于成本和fprime对于梯度 我可能必须执行两次操作 非常昂贵 grad and cost被计算 此外 在它们之间共享变量可
  • 在Python中从大文件中搜索单词列表

    我是新蟒蛇 我有一个单词列表和一个非常大的文件 我想删除文件中包含单词列表中的单词的行 单词列表按排序给出 并且可以在初始化期间输入 我正在努力寻找解决这个问题的最佳方法 我现在正在进行线性搜索 这花费了太多时间 有什么建议么 您可以使用i
  • Matplotlib:如何有效地将大量线段着色为独立渐变

    Python 绘图库 如何有效地将大量线段着色为独立渐变 已经 阅读this https stackoverflow com questions 8500700 how to plot a gradient color line in ma
  • Series.sort() 和 Series.order() 有什么区别?

    s pd Series nr randint 0 10 5 index nr randint 0 10 5 s Output 1 3 7 6 2 0 9 7 1 6 order 按值排序并返回一个新系列 s order Output 2 0
  • 协程从未被等待

    我正在使用一个简单的上下文管理器 其中包含一个异步循环 class Runner def init self self loop asyncio get event loop def enter self return self def e
  • AttributeError:“模块”对象没有属性[重复]

    这个问题在这里已经有答案了 我有两个 python 模块 a py import b def hello print hello print a py print hello print b hi b py import a def hi
  • 如何使用scrapy检查网站是否支持http、htts和www前缀

    我正在使用 scrapy 来检查某些网站是否工作正常 当我使用http example com https example com or http www example com 当我创建 scrapy 请求时 它工作正常 例如 在我的pa
  • python 中的 <> 运算符有什么作用?

    我刚刚遇到这个here http www feedparser org feedparser py 总是这样使用 if string1 find string2 lt gt 1 pass 什么是 lt gt 运算符这样做 为什么不使用通常的
  • 根据其他单元格值更改多个单元格值

    我想更改包含的单元格moving to movingToOpenor movingToClose基于下一个单元格中给出的状态 有时循环会被中断并且不会从open to close or close to open 这是我当前的数据框 Dat
  • Paste.httpserver 并通过 HTTP/1.1 Keep-alive 减慢速度;使用 httperf 和 ab 进行测试

    我有一个基于paste httpserver 的Web 服务器作为HTTP 和WSGI 之间的适配器 当我使用 httperf 进行性能测量时 如果每次使用 num conn 启动一个新请求 我每秒可以执行超过 1 000 个请求 如果我使
  • 使用 for 循环创建一系列元组

    我已经搜索过 但找不到答案 尽管我确信它已经存在了 我对 python 很陌生 但我以前用其他语言做过这种事情 我正在以行形式读取数据文件 我想将每行数据存储在它自己的元组中 以便在 for 循环之外访问 tup i inLine wher
  • pandas 相当于 np.where

    np where具有向量化 if else 的语义 类似于 Apache Spark 的when otherwise数据帧方法 我知道我可以使用np where on pandas Series but pandas通常定义自己的 API
  • 是否需要关闭没有引用它们的文件?

    作为一个完全的编程初学者 我试图理解打开和关闭文件的基本概念 我正在做的一项练习是创建一个脚本 允许我将内容从一个文件复制到另一个文件 in file open from file indata in file read out file
  • 无法通过 Python 子进程进行 SSH

    我需要通过堡垒 ssh 进入机器 因此 该命令相当长 ssh i
  • 如何将带有参数的Python装饰器实现为类?

    我正在尝试实现一个接受一些参数的装饰器 通常带有参数的装饰器被实现为双重嵌套闭包 如下所示 def mydecorator param1 param2 do something with params def wrapper fn def
  • 无法安装最新版本的 Numpy (1.22.3)

    我正在尝试安装最新版本的 numpy 即 1 22 3 但看起来 pip 无法找到最后一个版本 我知道我可以从源代码本地安装它 但我想了解为什么我无法使用 pip 安装它 PS 我有最新版本的pip 22 0 4 ERROR Could n

随机推荐