【问题标题】:Reading from .docx file results in a newline though the file is not just a newline从 .docx 文件中读取会产生换行符,尽管该文件不仅仅是换行符
【发布时间】:2021-12-09 20:33:25
【问题描述】:

我有一些文本存储在.docx 文件中:

Dear xx01xx,

    Thank you for coming to my birthday party. I’m glad you came. Thanks for the xx02xx present!

            Your best friend,
                Alan Bagel

If you want to see my token of appreciation for your coming, please go to xx03xx street.

现在当我尝试从中读取文本时:

import docx

template = docx.Document("...")
for paragraph in template.paragraphs:
    print(paragraph.text)

结果只是一个换行符!对.docx 文件的支持是否如此有限,以至于会被文件行上较小的字体大小混淆?

【问题讨论】:

  • 如果文本在表格中,即使是没有边框的表格,paragraph.text 也不会得到它。尝试遍历 template.tables。
  • @CodeMonkey 表中没有数据。
  • 检查修订状态。 python-docx 不会在未接受的修订中选择段落。使用 print(template._element) 查看 XML 并查看是否有任何元素正在“包装”段落。

标签: python file python-docx paragraph


【解决方案1】:

问题是即使您创建了一个.docx 文件,但您忘记保存它,因此python-docx 无法读取它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-22
    • 1970-01-01
    • 1970-01-01
    • 2015-09-04
    • 2021-12-14
    • 1970-01-01
    相关资源
    最近更新 更多