Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to highlight the cell
#1
Hi,
I am comparing two Excell files, and if a cell from file1 != a cell in file2 print that, and highlight the cell different in file2 (with red font).

Name   Age
AAA    25 
BBB    12
CCC    42

Name   Age
AAA    25 
BBB    16
ACC    3
I use below code, I can print the rows differently, but how can I highlight the cell which is different in file2.



file1_name = "D:InputCSV1.csv"
file2_name = "D:\InputCSV2.csv"

f1= open(file1_name, 'r')
f2= open(file2_name, 'r')
f1_lines = f1.readlines()
f2_lines = f2.readlines()

for k,l in enumerate(f1_lines):
    if(f1_lines[k] in f2_lines):
        pass        
    else:
        print("INFO: " + f1_lines[k][:-1] +"::"+ f1_lines[k][:-1] + " ->mismatch"
Reply
#2
I was curious so I searched it. Bear in mind that most people won't do google searches for you. ;)

http://ozzmaker.com/add-colour-to-text-in-python/

You'll have to go from there, but it seems to be exactly what you want.
Reply
#3
I checked this link before I post, but I only want to highlight the cell in file2 which is not equal in file1.
Here,
16 --> red color font (because the value in file2 in row2 is 16 which is !=12 in file1)
In row3: in file2 the name is ACC which is != CCC in file1, age is 3 in file1 !=42 in file1), show both the cells in row 3 should be in red font.
Reply
#4
I'm just been trying to play with it, and like many in that pages comments, it doesn't work on my machine python3.8 on Windows 10. Did you have any luck?

They suggest "http://pypi.python.org/pypi/colorama" but I haven't tried it.

There is also "https://pypi.org/project/termcolor/" that I found and installed, but their example didn't work for me either.

Gosh, I've been trying. Who'd think this could be so hard?! lol

Tkinter or pygame can help you put your results to a window, but that seems like a stupidly complicated workaround.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  highlight the columns and capture amy83 0 800 Mar-24-2023, 07:19 PM
Last Post: amy83
  How to compare two columns and highlight the unique values of column two using pandas shubhamjainj 0 4,278 Feb-24-2020, 06:19 AM
Last Post: shubhamjainj
  Highlight and remove specific string of text itsalmade 5 3,510 Dec-11-2019, 11:58 PM
Last Post: micseydel
  [ library recommendation ] search & highlight target texts in PDF? smallabc 1 2,217 Nov-27-2019, 10:40 AM
Last Post: Larz60+
  Highlight text in pdf pramodb35 1 2,897 Aug-03-2019, 02:58 AM
Last Post: Larz60+
  Highlight/Underline a string | ValueError: zero length field name in format searching1 1 3,014 Jul-01-2019, 03:06 AM
Last Post: metulburr
  How to highlight html using python Prince_Bhatia 2 3,582 May-03-2019, 11:31 AM
Last Post: Prince_Bhatia

Forum Jump:

User Panel Messages

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