How can I write values from PowerFactory to Excel via Python?

Category:
Scripting
Answer

Microsoft Excel can be used by Python scripts if the “Python for Windows Extensions" PyWin32 (http://sourceforge.net/projects/pywin32/)package is installed, which includes Win32 API, COM support and Pythonwin extensions.

 Here you will find a simple example how to open Excel and how to write some text in it from python. For more examples please refer to python literature.

Additional example can be found under How can I write calculated values to Word by using Python?

import powerfactory

app=powerfactory.GetApplication()

from win32com import client

excel=client.Dispatch("Excel.Application")

excel.visible=True

wb=excel.Workbooks.Add()

wb.Worksheets[0].Name="Report.LDF"

ws=wb.Worksheets[0]

 LineObj=app.GetCalcRelevantObjects("*.ElmLne")

ldf=app.GetFromStudyCase("ComLdf")

ldf.Execute()

ws.Cells(1,1).Value="Line Name"

ws.Cells(1,2).Value="Line Length"

ws.Cells(1,3).Value="loading [%]"

for j,i in enumerate(LineObj):

   ws.Cells(j+1,1).Value=i.loc_name

   ws.Cells(j+1,2).Value=i.dline

   ws.Cells(j+1,3).Value=i.GetAttribute("c:loading")

Back