pythonpiprtf

FileNotFoundError: rtfparser python package


Any one experienced with this package, i have tried many thing and it does not work , i have checked whether the file exists or not many times, absolute | relative path, it does not work

D:\pythonCodePycharmProjects\carProject\venv\Scripts\python.exe D:\pythonCodePycharmProjects\carProject\main.py 
Traceback (most recent call last):
  File "D:\pythonCodePycharmProjects\carProject\main.py", line 10, in <module>
    parsed = parser.parse_file()
             ^^^^^^^^^^^^^^^^^^^
  File "D:\pythonCodePycharmProjects\carProject\venv\Lib\site-packages\rtfparse\parser.py", line 66, in parse_file
    file = open(self.rtf_path, mode="rb")
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
FileNotFoundError: [Errno 2] No such file or directory: 'D:\\pythonCodePycharmProjects\\carProject\\rtf\\test.rtf'

Process finished with exit code 1
import pathlib
from rtfparse.parser import Rtf_Parser
from rtfparse.renderers import de_encapsulate_html

source_path = pathlib.Path(r"D:\pythonCodePycharmProjects\carProject\rtf\test.rtf")
target_path = pathlib.Path(r"D:\pythonCodePycharmProjects\carProject\rtf\test.html")


parser = Rtf_Parser(rtf_path=source_path)
parsed = parser.parse_file()


renderer = de_encapsulate_html.De_encapsulate_HTML()
with open(target_path, mode="w", encoding="utf-8") as html_file:
    renderer.render(parsed, html_file)

Solution

  • I do not know exactly why this works but, here is the solution, first create the .rtf file using notepad(New Text Document) after right clicking on the desktop instead of selecting rtf from the menu and paste the rtf there and be sure to name the file ending with .rtf.in the code add .txt at the end of the absolute path like below

    source_path = pathlib.Path(r"D:\pythonCodePycharmProjects\carProject\rtf\test1.rtf.txt")
    

    then it will parse the rtf and create or edit target_path and you can view the html code either on any editor or any browser. But it does not decode the html fully i mean it just displays the text/paragraph with out the styles applied like bold, size, font encoded in the rtf.

    how do i came about this solution, well i copied the path and paste it to file explorer and it opened it in wps and i guess may be i should be creating it using notepad i tried it does not work but one thing i notice is when paste the path in FE again it adds a .txt at the end of .rtf and it opens it in notepad.

    so, the output that give me

    Sharp costophrenic angles bilaterally . 
    
    There is no cardiac enlargement. 
    There is no active lung parenchymal lesion. 
    
    Impression:Normal chest xray