Páginas

jueves, 9 de octubre de 2014

Python: Crear directorios con os.mkdir()

Una cosa importante: Si se quiere crear un directorio con mkdir, se debe crear nivel por nivel
Si solo tengo el D:/ primero tengo que crear D:/Ruben y luego crear D:/Ruben/PL0001

from os import mkdir
from os.path import exists

destination = D:/Ruben/
pl = PL0001

if exists(destination): #si ya existiere el directorio
    if exists(destination+pl): #verifica si existe el subdirectorio
         destSubfolder = destination+pl
     else:
          mkdir(destination+pl)
#si no existe crea el subdirectorio
          destSubfolder = destination+pl
else: #si no existe el directorio
    mkdir(destination) #crea el directorio
    mkdir(destination+pl) #crea el subdirectorio
    destSubfolder = destination+pl

miércoles, 8 de octubre de 2014

Python: Extraer archivos adjuntos desde Outlook

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()


VBA Outlook: Como navegar por los objetos de Outlook usando VBA


El orden es el siguiente:

El NameSpace
Las carpetas
Las subcarpetas (si hay)
Los items
Bueno y todo lo que tengan los items, llegar hasta aquí era lo mas importante. Luego explorando los metodos y atributos que tiene cada objeto se pueden hacer maravillas.

Sub findPl()
'El Namespace es el objeto principal
Set ns = Outlook.GetNamespace("MAPI")
'Luego vienen los folders, los subfolders, y por ultimo los items:
For Each fold In ns.Folders
    For Each f In fold.Folders
        For Each it In f.Items
            MsgBox it.Subject
        Next it
    Next f
   
Next fold

End Sub