As we all know python think backslash's as a escape literal and finding rtf with out backslash's is impossible while this can be handled by prepending with the row 'r' or 'R' before the rtf string to tell python no no this backslash's are literal's not escape sequence.
The problem that I am facing is if the rtf is fetched from the mssql database and let us say I am accessing the rtf like an object how do I prepend the rtf string with the row 'r' or 'R' Or escape the backslash's by replacing with two backslash's?
Note that replacing in the database is impractical.
The library that I am using is https://pypi.org/project/striprtf/#description
Like I said with this it works.
from striprtf.striprtf import rtf_to_text
rtf = r"{\rtf1\fbidis\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fswiss\fprq2\fcharset0
Verdana;}{\f1\froman\fprq2\fcharset0 Times New Roman;}{\f2\fnil\fcharset0 Trebuchet MS;}}
\viewkind4\uc1\pard\ltrpar\f0\fs20 Sharp costophrenic angles bilaterally . \par \par
\pard\ltrpar\sl360\slmult1 There is no cardiac enlargement. \par There is no active lung
parenchymal lesion. \par \f1\fs28 \par \pard\ltrpar\f0\fs20 Impression:Normal chest xray\f2\fs20
\par }"
text = rtf_to_text(rtf)
print(text)
if i remove the r in the above code i get the below error and like i said i am getting the rtf string from the database and it is clearly going to be a variable so there is no way i can add r before it
D:\pythonCodePycharmProjects\carProject\venv\Scripts\python.exe D:\pythonCodePycharmProjects\carProject\main.py
File "D:\pythonCodePycharmProjects\carProject\main.py", line 3
ab There is no evidence for mass or lymphadenopathy in the abdomen or pelvis. \par \'b7\tab There is no evidence for ascites. \par \'b7\tab The visualized lung bases are clear. \par \'b7\tab Osseous structures are normal. \par \pard\ltrpar \par \b IMPRESSION\b0 : \par Localized fat stranding 2* to ? IBD \par \pard\ltrpar No evidence of urolithiasis\f1\fs20 \par }"
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 140-143: truncated \uXXXX escape
Process finished with exit code 1
But what if I am accessing the rtf like below:
from striprtf.striprtf import rtf_to_text
rtfFromDatabase = fetch the rtf from the database
rtf = r rtfFromDatabase // gives error
text = rtf_to_text(rtf)
print(text)
What I have tried, well I tried string concatination regexpression by replacing each \ with \.
But it gives error.