问题
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