| from openpyxl import load_workbook |
| from openpyxl.drawing.image import Image |
|
|
| def read_images_from_excel(file_path, filter_column): |
| # Load the workbook |
| wb = load_workbook(filename=file_path) |
|
|
| # Iterate through each sheet in the workbook |
| for sheet_name in wb.sheetnames: |
| sheet = wb[sheet_name] |
|
|
| # Find the column index of the filter column |
| filter_column_index = None |
| for col in range(1, sheet.max_column + 1): |
| if sheet.cell(row=1, column=col).value == filter_column: |
| filter_column_index = col |
| break |
|
|
| if filter_column_index is None: |
| print(f"Filter column '{filter_column}' not found in sheet '{sheet_name}'") |
| continue |
|
|
| # Iterate through each row in the sheet |
| for row in range(2, sheet.max_row + 1): |
| # Check if the filter condition is met |
| if sheet.cell(row=row, column=filter_column_index).value == "your_filter_value": |
| # Iterate through each image in the row |
| for image in sheet._images: |
| # Check if the image is in the same row as the filter condition |
| if image.anchor._from.row == row: |
| # Get the image data |
| img_data = image.image |
|
|
| # You can process the image data here |
| # For example, you can save the image to a file |
| img_data.save(f"{sheet_name}_{row}_{image.anchor._from.col}.png") |
|
|
| # Example usage: |
| read_images_from_excel("example.xlsx", "Column F") |
|
|