Python for Power Systems

A blog for power systems engineers to learn Python.

Export PSSE Data to Excel

Have you ever wanted to export data from PSSE into Microsoft Excel? In today’s post we’ll show you the exact steps that you need to export your data from PSSE directly into Microsoft Excel using Python.

If you want to see more tutorial posts like this: Tell us what topic you’d like covered using this form

Transcript

Hello Power systems engineers. Have you ever wanted to control Microsoft Excel from Python? What about exporting data from PSSE into Microsoft Excel? Well today you’ll learn how to do both of these things: Control Excel from Python and export from PSSE. Lets get started.

I’ve opened up my Python shell here. And I’ve pointed it at the PSSE installation that I’m running which is PSSE 32. I’ve added to the PYTHONPATH so sys.path.append which tells Python where to find the PSSE libraries. And the os.environ PATH addition tells PSSE where its own libraries are added. You will need both of these before you can start importing the next two steps.

First connection to Excel

So I’ve imported the excelpy module. This is distributed with PSSE version 32 and was written by the PSSE team. And it makes it easy to write data to Excel.

I’ll create an excelpy workbook. Now a workbook is just an Excel spreadsheet. And I’ll store a reference to that spreadsheet in a variable I am going to call xl. And I can show that new Excel spreadsheet and it appears on the right as book4.

Write a heading to Excel

I can write headings. So for instance to write a heading “bus numbers” to cell A1 I use the set_cell method. I can also set a heading in the second column to “voltage (p.u)”.

Export data from PSSE to Excel

Now lets export some data from PSSE and put here into Excel. I’m going to export two sets of data: – First all of the bus numbers in the demonstration saved case; – Followed by all of the per unit voltages for those bus numbers.

So first let’s get a list of all the bus numbers. First we’ll import psspy and import redirect.

We’ll use redirect.psse2py to change any text from appearing in pop up dialog boxes to being in the actual Python shell here. (Jervis notes If you forget to run redirect.psse2py you will see a series of incredibly annoying pop up boxes created by PSSE)

Now I’ve initialised PSSE and you can see the output has been put here into this PSSE shell rather than in any pop up boxes.

Now with PSSE initialised let’s load a case. And then read out a list of all the bus numbers. The bus numbers have been stored in a variable called buses.

I’ve used a function called abusint which is one of the subsystem data retrieval functions. The subsystem data retrieval functions let you access a whole host of data for all of the buses at once rather than one at a time. (Jervis notes the subsystem data retrieval functions are incredibly powerful – but can be difficult to use and understand because of the way PSSE wrote them. See our post on Designing an easier subsystem data retrieval API for an alternative that you can use yourself)

You can do the same to get the voltages. And here I’ve collected the voltages in per unit.

Now to write this column of buses and column of voltages into Excel. So I’m going to set the range from row 2 in column A to be the buses. And, the voltages in column B. Now I’ve used the zip function to transpose what was given to me by PSSE as a row into a column.

Now we can save our workbook.

So that’s it for now. If you have any questions about exporting data into Excel. Feel free to hop onto http://psspy.org and ask your question. I’d be happy to answer.

Ideas for Python videos.