Reading RTF file and manipulating the data inside that is tricky, it is depending upon the file you have, Hence I have tried all the above nothing worked, finally, the following code worked for me. Hope it will help those who are hunting for the solution.
from win32com.client import Dispatch
word = Dispatch('Word.Application') # Open word application
# word = DispatchEx('Word.Application') # start a separate process
word.Visible = 0 # Run in the background, no display
word.DisplayAlerts = 0 # No warning
path = r'C:\Projects\10.1\power.rtf'
doc = word.Documents.Open(FileName=path, Encoding='gbk')
for para in doc.paragraphs:
print(para.Range.Text)
doc.Close()
word.Quit()
If you want to store in a single variable, the following code will solve the problem.
from win32com.client import Dispatch
word = Dispatch('Word.Application') # Open word application
# word = DispatchEx('Word.Application') # start a separate process
word.Visible = 0 # Run in the background, no display
word.DisplayAlerts = 0 # No warning
path = r'C:\Projects\10.1\output_5.rtf' # Write absolute path, relative path will dial wrong
doc = word.Documents.Open(FileName=path, Encoding='gbk')
#for para in doc.paragraphs:
# print(para.Range.Text)
content = '\n'.join([para.Range.Text for para in doc.paragraphs])
print(content)
doc.Close()
word.Quit()