Python 打开多个excel文件,打开每个文件上的每个工作表,然后保存图像

Python 打开多个excel文件,打开每个文件上的每个工作表,然后保存图像,python,excel,python-3.x,xlsx,Python,Excel,Python 3.x,Xlsx,我有多个excel文件,里面有图像,这些图像位于不同的excel工作表上。我的目标是将图像保存到我的计算机上。这些图像稍后将用于人脸识别 我已经构建了一些代码来打开excel文件并抓取图像。但是,它只从一张表中获取,而不是从所有表中获取 import face_recognition import pandas as pd import win32com.client as win32 from PIL import ImageGrab import os #Read working dire

我有多个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')
导入人脸识别
作为pd进口熊猫
将win32com.client作为win32导入
从PIL导入ImageGrab
导入操作系统
#读取工作目录
打印(os.getcwd())#获取当前工作目录
os.chdir(“E:/DATA/Master DATA”)#设置工作目录
打印(os.getcwd())#检查更新的工作目录
#读取文件夹中的xlsx文件
path1=“E:/DATA/主数据”
files=os.listdir(路径1)
打印(文件)
listlength=len(文件)
#从每个xlsx文件提取数据
对于文件中的f:
excel=win32.gencache.EnsureDispatch('excel.Application')
计数=0

当(count变量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)

每个工作表都会重置变量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)

我已经添加了你正在做的事情的我的版本。我已经添加了你正在做的事情的我的版本。