1 | initial version |
I have the following script:
# get data loading branches, buses voltage, loads and machines.
import sys
import os
sys.path.insert(0, r"C:\Program Files (x86)\PTI\PSSE33\PSSBIN")
# xx --> substitute xx with Version Number here.
os.environ['PATH'] = r"C:\Program Files (x86)\PTI\PSSE33\PSSBIN" + ";" + os.environ['PATH']
import redirect
redirect.psse2py()
# this redirects PSS(R)E progress, report output to Python Shell/Console
import psspy
psspy.psseinit(50000)
kerja = 'D:\\DATA'
kasus = ('2018_1a.sav')
kes = kerja + '\\' + kasus
psspy.case(kes)
psspy.fdns([2,0,0,1,1,0,99,0])
#Branch
ierr,busnum = psspy.abrnint(flag=4,entry=2,string=['FROMNUMBER','TONUMBER'])
ierr,ckt = psspy.abrnchar(flag=4,entry=2,string='ID')
ierr,loading = psspy.abrnreal(flag=4,entry=2,string=['P','Q','PCTRATEA'])
#Trafo
# ierr,trbusnum = psspy.atrnint(string=['FROMNUMBER','TONUMBER'])
# ierr,trckt = psspy.atrnchar(string='ID')
# ierr,trloading = psspy.atrnreal(string=['P','Q','PCTRATEA'])
#Bus
ierr,bus = psspy.abusint(string='NUMBER')
ierr,name = psspy.abuschar(string='NAME')
ierr,volt = psspy.abusreal(string='KV')
#Load
ierr,bnum = psspy.aloadint(flag=1,string='NUMBER')
ierr,bname = psspy.aloadchar(flag=1,string='NAME')
ierr,bload = psspy.aloadcplx(flag=1,string=['MVANOM'])
#Machine
ierr,machnum = psspy.amachint(flag=4,string='NUMBER')
ierr,machname = psspy.amachchar(flag=4,string='NAME')
ierr,machload = psspy.amachreal(flag=4,string=['PGEN','QGEN'])
print 'Tahap 1 Run PSSE'
# x=len(trbusnum)
# print ('X = ' + str(x))
import excelpy
# create a Excel workbook for writing
xlobj = excelpy.workbook()
#=======================================
import pythoncom
import win32com
xl = win32com.client.Dispatch("Excel.Application")
# write header
xlobj.set_range(1,'b',['From bus','To bus','ID','MW','MVAr','PCTRATEA'])
xlobj.set_range(1,'i',['NUMBER','NAME','KV'])
xlobj.set_range(1,'m',['BNUM','BNAME','BLOAD'])
xlobj.set_range(1,'q',['MACHNUM','MACHNAME','PGEN','QGEN'])
# write data
xlobj.set_range(2,'b',zip(*busnum))
xlobj.set_range(2,'d',zip(*ckt))
xlobj.set_range(2,'e',zip(*loading))
xlobj.set_range(2,'i',zip(*bus))
xlobj.set_range(2,'j',zip(*name))
xlobj.set_range(2,'k',zip(*volt))
xlobj.set_range(2,'m',zip(*bnum))
xlobj.set_range(2,'n',zip(*bname))
xlobj.set_range(2,'o',zip(*bload))
xlobj.set_range(2,'q',zip(*machnum))
xlobj.set_range(2,'r',zip(*machname))
xlobj.set_range(2,'s',zip(*machload))
print 'Tahap 2 Olah Data di Excel'
# save workbook
import win32com
xl = win32com.client.Dispatch("Excel.Application")
xl.DisplayAlerts = False
xlobj.save(kerja + '\\' + 'scen1bfull.xlsx')
xl.DisplayAlerts = True
print 'Tahap 3 Save Data ke Excel'
# Cek Aplikasi Excel ada yang terbuka atau tidak
#=====================================================
try:
xl.Quit() #quit excel, as if user hit the close button/clicked file->exit.
wb = xl.ActiveWorkbook #Source https://mail.python.org/pipermail/python-win32/2011-August/011738.html
xl.Visible = 0
xl = None
del xl
del wb
except:
raise TypeError('Simpan dan Tutup File Excel atau End Task Ms Excel di Task Manager --> Background Process')
print 'Selesai'
pythoncom.CoUninitialize()
When I run this script, I got an error:
Tahap 1 Run PSSE
Traceback (most recent call last):
File "D:/DATA/myscript.py", line 75, in <module>
xlobj.set_range(2,'o',zip(*bload))
File ".\excelpy.py", line 828, in set_range
File "C:\Python27\lib\site-packages\win32com\client\__init__.py", line 474, in __setattr__
self._oleobj_.Invoke(*(args + (value,) + defArgs))
TypeError: Objects of type 'complex' can not be converted to a COM VARIANT
Process finished with exit code 1
Another issue: every time i run this script i have to kill manually excel on task manager, please help me fix my script.
Traceback (most recent call last):
File "D:/DATA/myscript.py", line 55, in <module>
xlobj = excelpy.workbook()
File ".\excelpy.py", line 1892, in workbook
File ".\excelpy.py", line 181, in __init__
File "C:\Python27\lib\site-packages\win32com\client\gencache.py", line 543, in EnsureDispatch
raise TypeError("This COM object can not automate the makepy process - please run makepy manually for this object")
TypeError: This COM object can not automate the makepy process - please run makepy manually for this object
Process finished with exit code 1