1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
| 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 = "F:\SIG"
outputDirectory = "images"
ensureDirExists(dataDirectory)
ensureDirExists(outputDirectory)
excel = win32.gencache.EnsureDispatch('Excel.Application')
excel.Visible = True
files = absoluteListDir(dataDirectory)
print('files en cours',files)
for file in files:
print("=" * 20)
print("Ovrir Workbook: ", file)
workbook = excel.Workbooks.Open(file)
for sheet in workbook.Sheets:
num=0
print("passage Sheet: ", sheet.Name)
for n, shape in enumerate(sheet.Shapes):
## variable char pour comptage en lettre si plusieurs photos
char = "A"
if shape.Name.startswith("Picture"):
shape.Copy()
image = ImageGrab.grabclipboard()
print('image =', image)
#outputFile = "{}/{}_{}_{}.png".format(outputDirectory, workbook.Name, sheet.Name, n
# Vérifier si le fichier existe ou non
outputFile = "{}/{}.png".format('',''+char,n)
#si image existe déja
if os.path.isfile( outputFile ):
#Comptage lettre
## conversion de caractère en entier
i = ord(char[0])
## incrementation
i += 1
## convertir le résultat int en char
char = chr(i)
outputFile = "{}/{}.png".format(''+char,n)
print("Fichier trouvé")
image.save(outputFile , "png")
print(outputFile)
#si image n'existe pas
else:
print("Fichier non trouvé")
image.save(outputFile , "png")
print(outputFile)
print("Fermeture Workbook")
workbook.Close(True) |
Partager