Reputation: 581
I want to format an output document with special characters such as \n
, \t
or \033
to change color of text, I am using a PDF library (PyPDF) to write a PDF, and need to bold a inline word, but the library is not so flexible with inline formatting, instead of it i tried to format with special characters as follows:
\033[1mBOLD TEXT\033[0m
but when I see the output PDF it is printed literal the string:
\033[1mBOLD TEXT\033[0m
instead of the bold text. How can I print non-ASCII characters to files? When I format the string and print it to console it is shown bold, but it is not bold on csv, txt, nor PDF.
Upvotes: 0
Views: 387
Reputation: 781
Unless PyPDF explicitly says it handles those special characters, you won't be able to make them do anything. ANSI escape codes are only intended for use in terminals, so it's not surprising they don't work anywhere else.
Bold text in PDFs works by switching the font used for rendering to a bold version of the font, so it's a bit more complicated if you don't have the library handling it for you. Depending on how you are adding content to the PDF, you might be able to insert the change font command into the document if you have the bold version of the font into the document (see this related question). Keep in mind that this is a deep rabbit hole, but if you feel brave you can take a look at the PDF spec (such as the reference for 1.7 found here).
More realistically I would recommend you switch libraries to something like fpdf2
which has support for parsing markdown.
Upvotes: 1