0

I will like to know how can I write some data in a txt or rtf file but with some colour. The aim is the next:

I am reading txt files and looking for some words in them. Once I find the words, what i have to do is put them with different colours (its like selecting) and save them in a new txt or rtf file (could be the same as original file).

Im currently using Python 3.9.7.

Thank you so much!

  • Have you tried the method described here: https://stackoverflow.com/questions/16162036/is-there-a-way-to-write-formatted-text-from-python – mirrorkeydev Dec 05 '21 at 22:56
  • Yes, I did. Actually i can set a defined word in Bold, but I can not write variables. For instance, I can not create a integer variable called "number" and set it to Bold – Pablo Carrero Garcia Dec 06 '21 at 13:03
  • When you say you can't set a variable to bold, do you mean the name of the variable (`number`), or the value of the variable (e.g. `5`)? If you want to write the name, this cannot be done indirectly (see https://stackoverflow.com/a/3683258/11319058), but you can type out the string manually as `"number"`. If you mean the value of the variable, you might need to convert it to a string first (i.e. `str(number)` -> `"5"`) – mirrorkeydev Dec 07 '21 at 05:24
  • Thank you all! I got the answer... https://stackoverflow.com/questions/52295971/python-using-a-variable-when-creating-an-rtf-file – Pablo Carrero Garcia Dec 07 '21 at 12:36

0 Answers0