python - 使用python从Excel文件中提取图像
问题描述
我有一个 100 行的 Excel 表。每个都有不同的信息,包括一个 id 和一个包含照片的单元格。
我使用 pandas 将数据加载到字典中:
import pandas as pd
df = pd.read_excel('myfile.xlsx')
data = []
for index,row in df.iterrows():
data.append({
'id':row['id'],
'field2':row['field2'],
'field3':row['field3']
})
对于图像列,我想提取每个图像,用行的 id 命名(image_row['id'].jpg)并将其放入文件夹中。然后,我想存储图像的路径,如下所示:
for index,row in df.iterrows():
data.append({
'id':row['id'],
'field2':row['field2'],
'field3':row['field3'],
'image':'path/image_'+row['id']+'.jpg'
})
我正在寻找一种方法来做到这一点,或者如果更好的话。你有什么主意吗 ?
我在 Linux 上,所以我不能将此方法与 pywin32 一起使用。
非常感谢
- 编辑
你可以在这里找到我使用的工作表示例
解决方案
我找到了使用openpyxl和openpyxl-image-loader模块的解决方案
# installing the modules
pip3 install openpyxl
pip3 install openpyxl-image-loader
然后,在脚本中:
#Importing the modules
import openpyxl
from openpyxl_image_loader import SheetImageLoader
#loading the Excel File and the sheet
pxl_doc = openpyxl.load_workbook('myfile.xlsx')
sheet = pxl_doc['Sheet_name']
#calling the image_loader
image_loader = SheetImageLoader(sheet)
#get the image (put the cell you need instead of 'A1')
image = image_loader.get('A1')
#showing the image
image.show()
#saving the image
image.save('my_path/image_name.jpg')
最后,我可以将路径和图像名称存储在字典中的每一行的循环中
推荐阅读
- mysql - SQL:选择表中特定字段集重复的所有条目
- oracle - 不同账户中的 Amazon RDS Oracle 数据库快照还原
- ruby-on-rails - 能够获得最小值,单个查询中的记录数
- kotlin - Is it safe to use Check and Require in production code? - Kotlin
- npm - 如何在未安装 cdk 时使用上下文参数执行 cdk 命令,即使用 npm run cdk?
- java - 将 Java HashMap 转换为海龟三元组
- python - mpdl3 和 lmfit 安装在终端中的 conda 成功,但在 spyder 中导入时未找到
- javascript - Number() 如果为 null 或为空,则默认为 0
- php - “分组” PHP 类的正确方法
- html - 将 div 放在两个 div 之间的边界之上