我有多个包含图像的excel文件,这些图像位于不同的excel表上。我的目标是将图像保存到计算机中。这些图像将在以后用于面部识别。
我已经构造了一些代码来打开excel文件并获取图像。但是,它仅取自一张纸,而不是全部纸。
import face_recognition
import pandas as pd
import win32com.client as win32
from PIL import ImageGrab
import os
#Read working directory
print(os.getcwd()) #get current working directory
os.chdir("E:/DATA/Master data") #set working directory
print(os.getcwd())#check updated working directory
#Reading xlsx file in a folder
path1="E:/DATA/Master data"
files= os.listdir(path1)
print(files)
listlength = len(files)
#Extracting data from each xlsx file
for f in files:
excel = win32.gencache.EnsureDispatch('Excel.Application')
count=0
while (count<listlength):
a = files.pop(count)
path_name = path1 + "/" + a
workbook = excel.Workbooks.Open(path_name)
wb_folder = workbook.Path
wb_name = workbook.Name
wb_path = os.path.join(wb_folder, wb_name)
for sheet in workbook.Worksheets:
for i, shape in enumerate(sheet.Shapes):
if shape.Name.startswith('Picture'):
shape.Copy()
image = ImageGrab.grabclipboard()
image.save('{}.jpg'.format(i+1), 'jpeg')
我希望从多个Excel文件的每张纸上获取所有图像。
答案 0 :(得分:0)
每张纸都将重置变量i,因此您的文件名相同,因此文件被覆盖。添加第二个变量,该变量将为每张工作表递增,因此文件名也包括该变量。
这已经过测试,可以正常工作,我添加了excel.Visible,因此您可以看到工作表弹出窗口:)还可以记录日志,以便您查看正在发生的情况。我没有使用全局计数变量,而是将工作簿名称连接到工作表名称,然后使用每个工作表图像中的“ n”变量。
import win32com.client as win32
from PIL import ImageGrab
import os
def ensureDirExists(filePath):
if not os.path.exists(filePath):
os.makedirs(filePath)
def absoluteListDir(directory):
for dirpath,_,filenames in os.walk(directory):
for f in filenames:
yield os.path.abspath(os.path.join(dirpath, f))
dataDirectory = "data"
outputDirectory = "images"
ensureDirExists(dataDirectory)
ensureDirExists(outputDirectory)
excel = win32.gencache.EnsureDispatch('Excel.Application')
excel.Visible = True
files = absoluteListDir(dataDirectory)
for file in files:
print("=" * 20)
print("Opening Workbook: ", file)
workbook = excel.Workbooks.Open(file)
for sheet in workbook.Sheets:
print("Scraping Sheet: ", sheet.Name)
for n, shape in enumerate(sheet.Shapes):
if shape.Name.startswith("Picture"):
shape.Copy()
image = ImageGrab.grabclipboard()
outputFile = "{}/{}_{}_{}.jpg".format(outputDirectory, workbook.Name, sheet.Name, n)
print("Saving Image File: ", outputFile)
image.save(outputFile, "jpeg")
print("Closing Workbook")
workbook.Close(True)