![](/img/trans.png)
[英]How can I use Python 3 and pandas to extract and put together same row numbers from multiple excel sheets?
[英]Use python extract images from Excel sheets
我找到了一些 Python2 代码来从 Excel 文件中提取图像。
我有一个非常基本的问题:我应该在哪里指定目标 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 文件中抓取图像:
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 子文件夹中获取图像。 您可以使用 ZipFile 类在 python 中执行此操作。 您不需要有 MS Excel,甚至不需要在 Windows 中运行!
文件路径和文件名在此处的变量中定义:
wb_folder = workbook.Path
wb_name = workbook.Name
wb_path = os.path.join(wb_folder, wb_name)
在这种特殊情况下,它在前面的行调用活动工作簿:
workbook = excel.ActiveWorkbook
但理论上您应该能够使用wb_folder
和wb_name
变量指定路径,只要您将文件加载到 excel 模块( Python: Open Excel Workbook using Win32 COM Api )。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.