Raj*_*Nha 9 python rtf python-3.x
我有一个 .rtf 文件,我想通过使用任何包来读取该文件并将字符串存储到使用 python3 的列表中,但它应该与 Windows 和 Linux 兼容。
我尝试过 striprtf 但 read_rtf 不起作用。
from striprtf.striprtf import rtf_to_text
from striprtf.striprtf import read_rtf
rtf = read_rtf("file.rtf")
text = rtf_to_text(rtf)
print(text)
Run Code Online (Sandbox Code Playgroud)
但在这段代码中,错误是:cannot import name 'read_rtf'
请有人建议任何从 python3 中的 .rtf 文件获取字符串的方法吗?
你试过这个吗?
with open('yourfile.rtf', 'r') as file:
text = file.read()
print(text)
Run Code Online (Sandbox Code Playgroud)
对于超大文件,请尝试以下操作:
with open("yourfile.rtf") as infile:
for line in infile:
do_something_with(line)
Run Code Online (Sandbox Code Playgroud)
小智 7
在 Python 中使用rtf_to_text
足以转换RTFinto
字符串。从 RTF 文件中读取内容,然后将其提供给rtf_to_text
:
from striprtf.striprtf import rtf_to_text
with open("yourfile.rtf") as infile:
content = infile.read()
text = rtf_to_text(content)
print(text)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
36755 次 |
最近记录: |