#CODIGO PARA AUTOMATIZAR SAP
import win32com.client
import subprocess
import time
import sys
import pandas as pd
import matplotlib.pyplot as plt
#import pyperclip
from tkinter import *
from tkinter import messagebox
"""AGR_1251
AGR_USER
USR02
UST04
UST12
UST10S
Exsopsap
PwC08.2023*
Aza (QAS)
vhazaqasci.us1.sap.aza.cl
00
QAS
/H/190.215.107.14"""
class SapGui(object):
def __init__(self):
#################### Abrimos Sap Logon ################################
self.path = "C:\Program Files (x86)\SAP\FrontEnd\SAPgui\saplogon.exe"
subprocess.Popen(self.path)
time.sleep(4)
self.SapGuiAuto = win32com.client.GetObject("SAPGUI")
application = self.SapGuiAuto.GetScriptingEngine
##################### Ingresamos a la conexión
################################
self.connection = application.OpenConnection("Aza (QAS)", True)
time.sleep(3)
self.session = self.connection.Children(0)
self.session.findById("wnd[0]").maximize
def sapLogin(self):
#################### Ingresamos datos de conexión
################################
try:
self.session.findById("wnd[0]/usr/txtRSYST-MANDT").text = "200"
self.session.findById("wnd[0]/usr/txtRSYST-BNAME").text = "Exsopsap"
self.session.findById("wnd[0]/usr/pwdRSYST-BCODE").text = "PwC08.2023*"
self.session.findById("wnd[0]/usr/txtRSYST-LANGU").text = "ES"
self.session.findById("wnd[0]").sendVKey(0)
time.sleep(2)
except:
#################### Manejo de errores ################################
print(sys.exc_info()[0])
#messagebox.showinfo("showinfo", "Login realizado correctamente!")
self.SearchData()
def SearchData(self):
#################### Extraemos la información de la tabla AGR_1251 en el
portapapeles ################################
self.session.findById("wnd[0]").maximize
self.session.findById("wnd[0]/tbar[0]/okcd").text = "se37"
self.session.findById("wnd[0]").sendVKey(0)
self.session.findById("wnd[0]/usr/ctxtRS38L-NAME").text = "RFC_READ_TABLE"
self.session.findById("wnd[0]/usr/ctxtRS38L-NAME").caretPosition = 14
self.session.findById("wnd[0]").sendVKey(8)
self.session.findById("wnd[0]/usr/txt[34,9]").text = "AGR_1251"
self.session.findById("wnd[0]/usr/txt[34,10]").text = "|"
self.session.findById("wnd[0]/usr/txt[34,12]").text = "100"
self.session.findById("wnd[0]/usr/txt[34,13]").text = "100"
self.session.findById("wnd[0]/usr/txt[34,13]").setFocus()
self.session.findById("wnd[0]/usr/txt[34,13]").caretPosition = 3
self.session.findById("wnd[0]/tbar[1]/btn[8]").press()
self.session.findById("wnd[0]/usr/lbl[34,25]").setFocus()
self.session.findById("wnd[0]/usr/lbl[34,25]").caretPosition = 0
self.session.findById("wnd[0]").sendVKey(2)
self.session.findById("wnd[0]/mbar/menu[0]/menu[1]").select()
self.session.findById("wnd[1]/usr/subSUBSCREEN_STEPLOOP:SAPLSPO5:0150/
sub:SAPLSPO5:0150/radSPOPLI-SELFLAG[1,0]").select()
self.session.findById("wnd[1]/usr/subSUBSCREEN_STEPLOOP:SAPLSPO5:0150/
sub:SAPLSPO5:0150/radSPOPLI-SELFLAG[1,0]").setFocus()
self.session.findById("wnd[1]/tbar[0]/btn[0]").press()
self.session.findById("wnd[1]/usr/ctxtDY_FILENAME").text = "Reporte
AGR_1251.txt"
self.session.findById("wnd[1]/usr/ctxtDY_FILENAME").caretPosition = 20
self.session.findById("wnd[1]/tbar[0]/btn[11]").press()
self.session.findById("wnd[0]/tbar[0]/btn[15]").press()
self.session.findById("wnd[0]/tbar[0]/btn[15]").press()
self.session.findById("wnd[0]/tbar[0]/btn[15]").press()
self.dashboard()
def dashboard(self):
#with open('Reporte AGR_1251.txt', 'w+') as report:
#report.write(pyperclip.paste())
df = pd.read_excel('AGR_NAME.XLSX')
fig1, ax1 =plt.subplots()
ax1.bar(sales_data.keys())
#df.drop("Unnamed: 0",axis=1, inplace=True)
#df.drop("Unnamed: 11",axis=1, inplace=True)
#df.dropna(inplace=True)
#result = df.values.tolist()
#print(result)
if __name__ == '__main__':
window = Tk()
window.geometry("200x50")
boton = Button(window, text= 'Loggear Sap Logon', command=
lambda :SapGui().sapLogin())
boton2 = Button(window, text= 'Descarga tablas actualizadas SAP', command=
lambda :SapGui().SearchData())
boton3 = Button(window, text= 'DASHBOARD', command=
lambda :SapGui().dashboard())
boton3.pack()
boton2.pack()
boton.pack()
mainloop()