【问题标题】:how to get text from .doc file using python 2.7如何使用 python 2.7 从 .doc 文件中获取文本
【发布时间】:2018-10-01 08:04:37
【问题描述】:

我正在使用以下代码从 .docx 文件中提取文本

def getText(filename):
              doc = docx.Document(filename)
              fullText = []
              for para in doc.paragraphs:
                  fullText.append(para.text)
              return '\n'.join(fullText)
          data = getText(file_path)

现在,我还想在 pythonanywhere 上托管的 django rest api 中提取 .doc 文件。由于 api 在 pythonanywhere 上,我无法安装 textract 库和 antiword。那么,我该怎么做?

【问题讨论】:

    标签: python-2.7 pythonanywhere .doc


    【解决方案1】:

    abiword 安装在 PythonAnywhere 上:

    abiword --to=txt myfile.doc
    

    将生成一个名为 myfile.txt 的文件。

    【讨论】:

    • 当我尝试在虚拟环境中运行命令时出现以下错误。 ** (abiword:26945): WARNING **: clutter failed 0, get a life. error: XDG_RUNTIME_DIR not set in the environment.
    • 尝试将环境变量 XDG_RUNTIME_DIR 设置为 /tmp -- 例如。从 bash 使用 export XDG_RUNTIME_DIR=/tmp/ 或者如果您从 Python 代码调用它,请将其设置在您用来启动它的调用中。
    • 如何在 python 脚本中运行命令。 subprocess.call('abiword --to=txt _file_path') 给出以下错误 Exception Type: OSError Exception Value: [Errno 2] No such file or directory
    • 首先您需要使用 abiword 的完整路径。在 PythonAnywhere 上是 /usr/bin/abiword。此外, subprocess.call 的参数不是字符串,而是列表。检查文档
    • 感谢您的帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-21
    • 1970-01-01
    • 1970-01-01
    • 2018-10-30
    • 1970-01-01
    相关资源
    最近更新 更多