Python Forum

Full Version: How to highlight the cell
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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"
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.
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.
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.