How do I export a graphic using Python?

Category:
Scripting
Answer

There are two ways of exporting shown plots using Python:

  1. By using the command object ComWr (for emf, svg, pdf, png, jpg, wmf, ...)
  2. By using the WriteWMF() method on SetDesktop (Graphics Board)

Both of them are export the currently shown plot with the difference that ComWr is a command object, which has to be defined and later executed, but offers more file types for the export. On the other hand WriteWMF() is a method, which only allows to export as a wmf.

The following examples export graphics pages such as plots. If the goal is to export other graphics (for example network diagrams), the "GrpPage" in the script must be replaced by "SetDeskpage".

The first Python code example exports the currently shown graphic using ComWr:

import powerfactory as pf
app = pf.GetApplication()

# Define export format (other options: emf, svg, pdf, png, jpg, ...)
iopt_rd = 'wmf'

# Get and set export command
ComWr = app.GetFromStudyCase('ComWr')
ComWr = app.GetFromStudyCase('ComWr')
ComWr.SetAttribute('iopt_rd', iopt_rd)
ComWr.SetAttribute('iopt_savas', 0) # 0 = Write to path, 1 = Open Save Dialog
ComWr.SetAttribute('f', f'D:\\tmp\plot.{iopt_rd}')

ComWr.Execute()

The second example exports all available plot pages (GrpPage) in the activated study case using SetDesktop.WriteWMF():

import powerfactory
app = powerfactory.GetApplication()

# Get active Graphics Board, which contains the plots
SetDesktop = app.GetGraphicsBoard()

# Iterate over pages and export
for GrpPage in SetDesktop.GetContents('*.GrpPage'):
    SetDesktop.Show(GrpPage)
    SetDesktop.WriteWMF(f'D:\\tmp\\{GrpPage.GetAttribute("loc_name")}')

 

Note:

For PowerFactory 2020 and earlier versions the graphic objects (GrpPage) must be exchanged with respective VI-objects (virtual instrument).

 

Back