Revising xlsx file

lingjun_jiang_c2fc6902f7e

Lingjun Jiang

Posted on October 15, 2024

Revising xlsx file
import openpyxl

# Load the workbook and select the active worksheet
workbook = openpyxl.load_workbook('Lab_3.xlsx')
sheet = workbook.active

# Function to get the RGB color of a cell fill
def get_rgb_color(cell):
    if cell.fill.start_color.index == '00000000':  # cell.fill is a complex object
        return None
    return cell.fill.start_color.rgb # so we just need to access the start_color attribute

# Iterate through the rows in the worksheet
count = 0
reference_color = None
for row in sheet.iter_rows(min_row=1, min_col=1, max_col=3):
    count += 1
    cell = row[1]  # Access the second column in the row
    if count == 1:
        reference_color = get_rgb_color(cell)  # Get the fill color of the reference cell
        continue
    cell_color = get_rgb_color(cell)
    if cell_color == reference_color:
        sheet.cell(row=count, column=4, value=1)  # Set value to 1 in the new column (D)
    else:
        sheet.cell(row=count, column=4, value=0)  # Set value to 0 in the new column (D)

# Save the workbook
workbook.save('Lab_3_updated.xlsx')


Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
lingjun_jiang_c2fc6902f7e
Lingjun Jiang

Posted on October 15, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related