Digamos que tengo 1000 numeros de PL y mi base de datos es mi correo electronico, con este script puedo crear una carpeta con todos los documentos que correspondan a dicha importacion:
from win32com import client
from os import mkdir
from os.path import exists
pl = "0002181407160MHMA07K" #el numero de referencia a buscar en el mail
destination = 'D:/' #la carpeta raiz de destino
try:
ol= client.Dispatch("Outlook.Application") #objeto de outlook
ns = ol.GetNameSpace("MAPI") #objeto NameSpace
except:
print "There was a problem accessing Outlook"
raw_input()
#crea un directorio con el nombre del contrato, si es que no existe ya.
if exists(destination+pl):
destSubfolder = destination+pl
else:
mkdir(destination+pl)
destSubfolder = destination+pl
try:
counter = 0 #contará el numero de attachments encontrados
print 'Extracting attachments from %s...' %pl
for fold in ns.folders: #busca en cada carpeta
for f in fold.folders: #busca en la coleccion de subcarpetas
if f.Name == "Inbox": #solo en las subcarpetas Inbox
for it in f.items: #por cada mail en la colecion de mail items
if pl in it.Subject: #si encuentra el pl en el asunto
attNumber = it.Attachments.Count #the number of attachments
if attNumber > 0: #only if there are attachments in the msg
for n in range(attNumber):
att = it.Attachments.Item(n+1) # the file
attName = att.DisplayName #the file name
if not 'image' in attName: #excludes image.001 files
print attName
att.SaveAsfile(destSubfolder+ '/'+ attName)
counter +=1
#crea una carpeta con el nombre del pl en el root
print "Total attacments extracted: %i" %counter
raw_input()
except:
print "There was a problem extracting files"
raw_input()
from win32com import client
from os import mkdir
from os.path import exists
pl = "0002181407160MHMA07K" #el numero de referencia a buscar en el mail
destination = 'D:/' #la carpeta raiz de destino
try:
ol= client.Dispatch("Outlook.Application") #objeto de outlook
ns = ol.GetNameSpace("MAPI") #objeto NameSpace
except:
print "There was a problem accessing Outlook"
raw_input()
#crea un directorio con el nombre del contrato, si es que no existe ya.
if exists(destination+pl):
destSubfolder = destination+pl
else:
mkdir(destination+pl)
destSubfolder = destination+pl
try:
counter = 0 #contará el numero de attachments encontrados
print 'Extracting attachments from %s...' %pl
for fold in ns.folders: #busca en cada carpeta
for f in fold.folders: #busca en la coleccion de subcarpetas
if f.Name == "Inbox": #solo en las subcarpetas Inbox
for it in f.items: #por cada mail en la colecion de mail items
if pl in it.Subject: #si encuentra el pl en el asunto
attNumber = it.Attachments.Count #the number of attachments
if attNumber > 0: #only if there are attachments in the msg
for n in range(attNumber):
att = it.Attachments.Item(n+1) # the file
attName = att.DisplayName #the file name
if not 'image' in attName: #excludes image.001 files
print attName
att.SaveAsfile(destSubfolder+ '/'+ attName)
counter +=1
#crea una carpeta con el nombre del pl en el root
print "Total attacments extracted: %i" %counter
raw_input()
except:
print "There was a problem extracting files"
raw_input()
No hay comentarios:
Publicar un comentario