Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Saving python cmd output into a text file with colours

from colorama import Fore, Back, Style, init
import fpdf
from fpdf import FPDF


paragraph = 'Whatever blame emerges! Its gasoline avoids the prison. The unlimited mania varies an exotic garage. A curtain farms? The sunrise fells an assorted companion around the jet.'

key_words = ['blame', 'avoids', 'unlimited', 'exotic']

def highlight(word):
    if word in key_words:
        return Fore.RED + str(word) + Fore.RESET
        return str(word)

textpara = paragraph.strip().split(' ')
colored_words = list(map(highlight, textpara))
final = " ".join(colored_words)
print (final)
I have created a code which highlight the keywords in a paragraph. I ran it in the cmd and it works fine (output is in the attachment). How do I save this output into file or PDF with colours highlighted so I don't have to re run the code again?

I tried to copy and paste the output into a text file but the colours did not show up

Thanks for the help

Attached Files Thumbnail(s)
I'm not sure that's possible. I think if you want to save colored text you may need to do it in another format, such as HTML with tags to color the text.
Craig "Ichabod" O'Brien -
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

i think the easiest way would be to save it as an html file. Change the paragraph section to encompass the keywords with bold since you dont have more than one color <b>keyword</b>. And if you needed more colors wrap in font tags like: <font color="red">This is some text!</font>

Then just open the file in a browser to view it.

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Import Text, output curve geometry Alyner 0 161 Feb-03-2020, 03:05 AM
Last Post: Alyner
  Saving a mask as a png file in opencv DanTheMan 1 114 Jan-31-2020, 09:20 AM
Last Post: ThiefOfTime
  csv file output rturus 7 162 Jan-30-2020, 02:09 PM
Last Post: buran
  Rename file from value in text file Nuge93 1 122 Jan-20-2020, 03:50 PM
Last Post: gruntfutuk
  File name as part of output file name Jeeping_Coder 1 127 Jan-10-2020, 03:43 PM
Last Post: Clunk_Head
  help with changing values in an excel sheet and saving to a new file Moneyman212 2 233 Nov-19-2019, 06:25 AM
Last Post: Moneyman212
  auto supply values to a python script(function) from text file metro17 4 260 Oct-26-2019, 01:25 AM
Last Post: ichabod801
  How to extract a matrix from .xml.gz file to a excel file or any other output? enyrb 0 174 Oct-21-2019, 01:01 PM
Last Post: enyrb
  Saving as docx file from excel with new filename Chuck_Norwich 4 314 Oct-03-2019, 08:01 PM
Last Post: Chuck_Norwich
  New to Python, help with saving image from camera tantony 2 720 Sep-13-2019, 05:19 PM
Last Post: tantony

Forum Jump:

Users browsing this thread: 1 Guest(s)