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 striprtf · PyPI
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)
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.