Python Forum
help with an exercise - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: help with an exercise (/thread-40575.html)



help with an exercise - iiiik - Aug-21-2023

I wanted to ask you about an exercise that tries to combine two columns in different excel sheets from python, that compares row by row if they are equal or not and if they are, print a 1 and if they are not equal a 0.


RE: help with an exercise - jefsummers - Aug-21-2023

Show us what you have done so far and where you are having problems.


RE: help with an exercise - iiiik - Aug-21-2023

This is what I have written in code, the problem is that the solution to my exercise is that it underlines the differences between the two sheets and I want it to print 1 if it is the same and 0 if it is different.
[attachment=2501]


RE: help with an exercise - snippsat - Aug-21-2023

Post code in code tag and not image(as can not test anyhing without rewrite all code).


RE: help with an exercise - iiiik - Aug-21-2023

import openpyxl
from openpyxl.styles import PatternFill

datafile1 = openpyxl.load_workbook("C:\\Users\\ime1s\\Downloads\\MiCalculo.xlsx")

fill_style = PatternFill(start_color="FDD835", end_color="FDD835", fill_type="solid")

data_sheet1 = datafile1["Calculo"]
data_sheet2 = datafile1["Calculo2"]

for i in data_sheet1.iter_rows():
    for cell in i:
        current_cell_value = cell.value
        cell_location = cell.coordinate
        
        if current_cell_value != data_sheet2[cell_location].value:
            cell.fill = fill_style
            
datafile1.save("compared_file.xlsx")
That's ok?


RE: help with an exercise - snippsat - Aug-21-2023

Change to this and see if it is better.
Take look at what zip do,then will understand the loop better.
import openpyxl
from openpyxl.styles import PatternFill

datafile1 = openpyxl.load_workbook('test1.xlsx')
fill_style1 = PatternFill(start_color='FDD835', end_color='FDD835', fill_type='solid')
fill_style2 = PatternFill(start_color='cc0000', end_color='cc0000', fill_type='solid')
data_sheet1 = datafile1['Sheet1']
data_sheet2 = datafile1['Sheet2']

# Create a new sheet for comparison results
comparison_sheet = datafile1.create_sheet('Comparison Results')

# Iterate through rows in both sheets and compare values
for row1, row2 in zip(
    data_sheet1.iter_rows(min_row=1, max_row=data_sheet1.max_row),
    data_sheet2.iter_rows(min_row=1, max_row=data_sheet2.max_row),
):
    for cell1, cell2 in zip(row1, row2):
        if cell1.value == cell2.value:
            comparison_sheet.cell(row=cell1.row, column=cell1.column, value=1).fill = fill_style1
        else:
            comparison_sheet.cell(row=cell1.row, column=cell1.column, value=0).fill = fill_style2

datafile1.save('compared_file.xlsx')



RE: help with an exercise - iiiik - Aug-21-2023

Thank you very much for the help!! It has helped me a lot.