【问题标题】:Issue reading text with python-docx when document contains Images当文档包含图像时,使用 python-docx 读取文本问题
【发布时间】:2014-06-28 01:21:31
【问题描述】:

我在解析包含图像的文档中的文本时遇到问题。

我在运行 Ubuntu 12.04.4 LTS(GNU/Linux 3.2.0-60-generic x86_64)的 Ubuntu Linux 机器上使用 Python docx 0.7.0 版

我正在使用这个逻辑:

```

        document = Document(path)
        # Get all paragraphs
        paras = document.paragraphs

        text = ""

        # Push the text from the paragraph on a single string
        for para in paras:
            # Don't forget the line break
            text += "\n" + para.text

        return text.strip()

```

当有图像时,此过程失败。

是不是我做错了什么?

【问题讨论】:

  • 您应在此处提供错误跟踪。 “失败”有很多含义。此外,请检查库的文档和已知问题,看看它们是否支持使用图像解析文档。

标签: python ubuntu docx python-docx


【解决方案1】:

python-docx 应该支持你在这里尝试做的事情。如果您提供在引发错误时获得的堆栈跟踪,我会看一看。

顺便说一句,您可以更优雅地编写代码:

document = Document(path)
text = '\n'.join([para.text for para in document.paragraphs])

【讨论】:

  • 我似乎无法用相同的文件让它失败。我将不得不做更多的调查,看看是什么导致了这个问题。我们对 python-docx 代码的调用包含在另一个使用 argparse 设置的脚本的调用中。我可能需要扩展子脚本的日志记录,看看是否能找出导致问题的原因。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-23
  • 2021-01-02
  • 2017-11-22
相关资源
最近更新 更多