【问题标题】:Reading in text from docx as paragraphs and saving paragraphs as string to a list从 docx 中读取文本作为段落并将段落作为字符串保存到列表中
【发布时间】:2020-06-10 18:12:22
【问题描述】:

我从一个 docx 文档中阅读了这些段落,这很有效。现在,我想将每个段落作为字符串存储到列表中。我写了下面的代码并运行它。

from docx import Document
sentences = []

document = Document('/path/to/*.docx')
for para in document.paragraphs:
    print(para.text)
    para = para.text
    sentences = sentences.append(para)

得到了这个错误:

AttributeError: 'NoneType' object has no attribute 'append'

有人知道如何得到我想要的输出吗?

【问题讨论】:

    标签: python string list text docx


    【解决方案1】:

    使用

    sentences.append(para)
    

    代替:

    sentences = sentences.append(para)
    

    List.append() 函数返回 None

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-28
      • 2020-01-16
      • 1970-01-01
      • 2015-04-16
      相关资源
      最近更新 更多