Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用python从Excel工作表中提取图像_Python_Excel_Image - Fatal编程技术网

使用python从Excel工作表中提取图像

使用python从Excel工作表中提取图像,python,excel,image,Python,Excel,Image,我找到了一些Python2代码来从Excel文件中提取图像 我有一个非常基本的问题:我应该在哪里指定目标excel文件的路径 import win32com.client # Need pywin32 from pip from PIL import ImageGrab # Need PIL as well import os excel = win32com.client.Dispatch("Excel.Application") workbook = excel.Acti

我找到了一些Python2代码来从Excel文件中提取图像

我有一个非常基本的问题:我应该在哪里指定目标excel文件的路径

import win32com.client       # Need pywin32 from pip
from PIL import ImageGrab    # Need PIL as well
import os

excel = win32com.client.Dispatch("Excel.Application")
workbook = excel.ActiveWorkbook

wb_folder = workbook.Path
wb_name = workbook.Name
wb_path = os.path.join(wb_folder, wb_name)

#print "Extracting images from %s" % wb_path
print("Extracting images from", wb_path)

image_no = 0

for sheet in workbook.Worksheets:
    for n, shape in enumerate(sheet.Shapes):
        if shape.Name.startswith("Picture"):
            # Some debug output for console
            image_no += 1
            print("---- Image No. %07i ----", image_no)

            # Sequence number the pictures, if there's more than one
            num = "" if n == 0 else "_%03i" % n

            filename = sheet.Name + num + ".jpg"
            file_path = os.path.join (wb_folder, filename)

            #print "Saving as %s" % file_path    # Debug output
            print('Saving as ', file_path)

            shape.Copy() # Copies from Excel to Windows clipboard

            # Use PIL (python imaging library) to save from Windows clipboard
            # to a file
            image = ImageGrab.grabclipboard()
            image.save(file_path,'jpeg')
或者,它是否仅适用于活动的打开的Excel文件

import win32com.client       # Need pywin32 from pip
from PIL import ImageGrab    # Need PIL as well
import os

excel = win32com.client.Dispatch("Excel.Application")
workbook = excel.ActiveWorkbook

wb_folder = workbook.Path
wb_name = workbook.Name
wb_path = os.path.join(wb_folder, wb_name)

#print "Extracting images from %s" % wb_path
print("Extracting images from", wb_path)

image_no = 0

for sheet in workbook.Worksheets:
    for n, shape in enumerate(sheet.Shapes):
        if shape.Name.startswith("Picture"):
            # Some debug output for console
            image_no += 1
            print("---- Image No. %07i ----", image_no)

            # Sequence number the pictures, if there's more than one
            num = "" if n == 0 else "_%03i" % n

            filename = sheet.Name + num + ".jpg"
            file_path = os.path.join (wb_folder, filename)

            #print "Saving as %s" % file_path    # Debug output
            print('Saving as ', file_path)

            shape.Copy() # Copies from Excel to Windows clipboard

            # Use PIL (python imaging library) to save from Windows clipboard
            # to a file
            image = ImageGrab.grabclipboard()
            image.save(file_path,'jpeg')

文件路径和文件名在以下变量中定义:

wb_folder = workbook.Path
wb_name = workbook.Name
wb_path = os.path.join(wb_folder, wb_name)
在这种特殊情况下,它在前一行调用活动工作簿:

workbook = excel.ActiveWorkbook

但从理论上讲,只要将文件加载到excel模块(),就可以使用
wb_文件夹
wb_名称
变量指定路径

您可以从现有Excel文件中获取图像,如下所示:

from PIL import ImageGrab
import win32com.client as win32

excel = win32.gencache.EnsureDispatch('Excel.Application')
workbook = excel.Workbooks.Open(r'C:\Users\file.xlsx')

for sheet in workbook.Worksheets:
    for i, shape in enumerate(sheet.Shapes):
        if shape.Name.startswith('Picture'):  # or try 'Image'
            shape.Copy()
            image = ImageGrab.grabclipboard()
            image.save('{}.jpg'.format(i+1), 'jpeg')

xlsx文件实际上是一个zip文件。您可以直接从xl/media子文件夹获取图像。您可以在python中使用ZipFile类来实现这一点。您不需要MS Excel,甚至不需要在Windows中运行

似乎有很多图片名称都不是以“图片”开头。我想如果我们仔细检查shape.names,就可以更好地控制提取哪些图像。非常感谢你的黑客攻击。