Created
June 24, 2025 03:31
-
-
Save suqingdong/afbead6aebd1e3a0f1bbe60680d5226f to your computer and use it in GitHub Desktop.
Python读取Excel单元格图像
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import openpyxl | |
| from PIL import Image | |
| wb = openpyxl.load_workbook('test.xlsx') | |
| ws = wb.active | |
| for image in ws._images: | |
| anchor = image.anchor | |
| if hasattr(anchor, '_from'): | |
| row = anchor._from.row + 1 | |
| col = anchor._from.col + 1 | |
| cell_address = ws.cell(row=row, column=col).coordinate | |
| print("图片位置:", cell_address) | |
| im = Image.open(image.ref) | |
| im.save(f'image_from_{cell_address}.png') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import openpyxl | |
| from openpyxl_image_loader import SheetImageLoader | |
| wb = openpyxl.load_workbook('test.xlsx') | |
| ws = wb.active | |
| image_loader = SheetImageLoader(ws) | |
| if image_loader.image_in('B7'): | |
| print("Image found in cell B7") | |
| image = image_loader.get('B7') | |
| image.save('image_from_B7.png') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment