Dimanche, février 22nd, 2009 | Author: admin

Voici la procédure pour générer une liste des fonctions et des constantes pouvant être utilisé avec la librairie win32com.

1)Installer win32com
2)Executer le script makepy.py dans le répertoire : PythonX.X\Lib\site-packages\win32com\client
3)Récupérer le fichier .py créer dans le répertoire PythonX.X\Lib\site-packages\win32com\gen_py (prendre le fichier le plus récent car le nom est composé de chiffre et de lettre). Son contenu contient une liste des fonctions et constante requise pour contrôle une application.

exemple

# -*- coding: mbcs -*-
# Created by makepy.py version 0.4.95
# By python version 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit (Intel)]
# From type library ‘EXCEL.EXE’
# On Wed Oct 31 07:55:10 2007
“”"Microsoft Excel 10.0 Object Library”"”
makepy_version = ‘0.4.95′
python_version = 0×20501f0

import win32com.client.CLSIDToClass, pythoncom
import win32com.client.util
from pywintypes import IID
from win32com.client import Dispatch

# The following 3 lines may need tweaking for the particular server
# Candidates are pythoncom.Missing, .Empty and .ArgNotFound
defaultNamedOptArg=pythoncom.Empty
defaultNamedNotOptArg=pythoncom.Empty
defaultUnnamedArg=pythoncom.Empty

CLSID = IID(’{00020813-0000-0000-C000-000000000046}’)
MajorVersion = 1
MinorVersion = 4
LibraryFlags = 8
LCID = 0×0

class constants:
xl3DBar =-4099 # from enum Constants
xl3DEffects1 =0xd # from enum Constants
xl3DEffects2 =0xe # from enum Constants
xl3DSurface =-4103 # from enum Constants
…..

Category: Python  | Leave a Comment
Dimanche, février 22nd, 2009 | Author: admin

Ici un exemple pour contrôle Excel avec Python en utilisant la librairie win32com.

import win32com.client

try:
 xlToRight =-4161 #Constante extrait de gen_py via makepy.py
 xlOr =0×2 #Constante extrait de gen_py via makepy.py
 excel = win32com.client.Dispatch(’Excel.Application’)
 excel.Columns(”C:C”).Select()
 excel.Selection.Insert(xlToRight)
 excel.Range(”C1″).FormulaR1C1 = “Type Dépense”
 excel.Range(”A1″).Select()
 excel.Selection.AutoFilter(4,’=595100′,xlOr,’=590020′) #FIltre avec deux conditions et un OR
 i=1
 while excel.Range(’D'+str(i)).FormulaR1C1<>””:
  if excel.Range(’D'+str(i)).FormulaR1C1==’595100′ or excel.Range(’D'+str(i)).FormulaR1C1==’590020′ :
   excel.Range(’C'+str(i)).FormulaR1C1=u’Intérêt’
  else:
   excel.Range(’C'+str(i)).FormulaR1C1=u’Projet’
  i=i+1
 excel.Selection.AutoFilter())
except:
 easygui.msgbox(”Erreur inconnue avec Excel”)
 sys.exit()

Category: Python  | Leave a Comment
Vendredi, février 06th, 2009 | Author: admin

Comment contrôler SAP avec Python via son scripting engine….

Facile via la librairie win32com pour python !

Tout ce qu’un usager fait, le code peut le faire…
Finit les lourdes tâches d’extraction ou d’insertions de données !

Voici la routine de base pour ouvrir une connection :

import win32com.client

p = subprocess.Popen(”c:\PROGRA~1\Sap\FrontEnd\SAPgui\saplgpad.exe”)#Lance SAP
Sap = True #Loop permet d’attendre l’ouvertured de Sap scripting
while Sap:
  try:
    SapGui = win32com.client.GetObject(”SAPGUI”)
    Sap = False
  except:
    pass
try:
  Applic = SapGui.GetScriptingEngine
  Connection = Applic.OpenConnection(’Production’, True)
except:
  print = “connection ou sap non disponible”
session = Connection.Children(0)
session.FindById(”wnd[0]/usr/txtRSYST-BNAME”).text = usr #entre le mot de passe
session.FindById(”wnd[0]/usr/pwdRSYST-BCODE”).text = pwd #entre le code d’utilisateur
#utiliser session pour trouver les contrôles utilisateur

liens intéressant sur le sujet

Category: Python  | Leave a Comment