How to read .rtf file and convert into python3 strings and can be stored in python3 list?

谁说胖子不能爱 提交于 2021-02-08 10:18:15

问题


I am having a .rtf file and I want to read the file and store strings into list using python3 by using any package but it should be compatible with both Windows and Linux.

I have tried striprtf but read_rtf is not working.

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)

But in this code, the error is: cannot import name 'read_rtf'

Please can anyone suggest any way to get strings from .rtf file in python3?


回答1:


Have you tried this?

with open('yourfile.rtf', 'r') as file:
    text = file.read()
print(text)

For a super large file, try this:

with open("yourfile.rtf") as infile:
    for line in infile:
        do_something_with(line)



回答2:


Try using this:

from striprtf.striprtf import rtf_to_text

sample_text = "any text as a string you want"
text = rtf_to_text(sample_text)


来源:https://stackoverflow.com/questions/60897366/how-to-read-rtf-file-and-convert-into-python3-strings-and-can-be-stored-in-pyth

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!