Python to extract value from specific cells

  Kiến thức lập trình

Raw Data

Output

I would like to extract text from cells in yellow and format it as the output image.

I tried the following code to extract data but after running it the ouput I get is [].

import openpyxl

# Load the spreadsheet
workbook = openpyxl.load_workbook('Apple.xlsx')
sheet = workbook.active

# Define the range of rows to extract text from
start_row = 22
end_row = 56

# Iterate over the cells in the specified range and pick up text from yellow cells
yellow_text = []
for row in range(start_row, end_row + 1):
    for column in sheet.iter_cols():
        cell = column[row-1]
        if cell.fill.start_color.index == 'FFFF00':
            yellow_text.append(cell.value)

# Display the extracted text
print(yellow_text)
import openpyxl

# Load the spreadsheet
workbook = openpyxl.load_workbook('your_spreadsheet.xlsx')
sheet = workbook.active

# Define the range of rows to extract text from
start_row = 22
end_row = 56

# Iterate over the cells in the specified range and pick up text from yellow cells
yellow_text = []
for row in range(start_row, end_row + 1):
    for column in sheet.iter_cols():
        cell = column[row-1]
        if cell.fill.start_color.index == 'FFFF00':
            yellow_text.append(cell.value)

# Display the extracted text
print(yellow_text)

New contributor

skatey is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

LEAVE A COMMENT