Python Forum
Saving python cmd output into a text file with colours
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Saving python cmd output into a text file with colours
#1
from colorama import Fore, Back, Style, init
import fpdf
from fpdf import FPDF


init()





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
    
    else:
        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)
   
Reply
#2
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 - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
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.
Recommended Tutorials:
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Replace a text/word in docx file using Python Devan 4 2,850 Oct-17-2023, 06:03 PM
Last Post: Devan
  save values permanently in python (perhaps not in a text file)? flash77 8 1,119 Jul-07-2023, 05:44 PM
Last Post: flash77
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,046 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
Thumbs Up Need to compare the Excel file name with a directory text file. veeran1991 1 1,064 Dec-15-2022, 04:32 PM
Last Post: Larz60+
  Saving the times a script is run to a file or ... 3Pinter 7 1,320 Oct-19-2022, 05:38 PM
Last Post: 3Pinter
  Code Assistance needed in saving the file MithunT 0 782 Oct-09-2022, 03:50 PM
Last Post: MithunT
  Saving the print result in a text file Calli 8 1,700 Sep-25-2022, 06:38 PM
Last Post: snippsat
  Saving progress in a Python program to use later Led_Zeppelin 9 2,052 Sep-11-2022, 01:32 PM
Last Post: snippsat
  Modify values in XML file by data from text file (without parsing) Paqqno 2 1,575 Apr-13-2022, 06:02 AM
Last Post: Paqqno
  Trying to determine attachment file type before saving off.. cubangt 1 2,093 Feb-23-2022, 07:45 PM
Last Post: cubangt

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020