Python 读取 RTF 文件文本内容:使用 python-docx 库
使用 Python 的 python-docx 库可以轻松地从 RTF 文件中读取所有文本内容。以下是详细步骤:
- 安装
python-docx库:
pip install python-docx
- 导入库并读取 RTF 文件:
import docx
doc = docx.Document('example.rtf')
- 遍历文档中的段落和表格,并获取其文本内容:
for para in doc.paragraphs:
print(para.text)
for table in doc.tables:
for row in table.rows:
for cell in row.cells:
print(cell.text)
通过以上步骤,您就可以轻松地读取 RTF 文件中的所有文本内容。
原文地址: https://www.cveoy.top/t/topic/oLf4 著作权归作者所有。请勿转载和采集!