How can I write calculated values to Word by using Python?

Category:
Scripting
Answer

You can access to Microsoft Word 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 create a small Table in Word and to fill it with date.  For more examples please refer to python literature.  

Additional example can be found under How can I write values from PowerFactory to Excel via Python?

from win32com import client
import powerfactory as pf
app=pf.GetApplication()
Lines=app.GetCalcRelevantObjects('*.ElmLne')
ldf=app.GetFromStudyCase('ComLdf')
ldf.Execute()
NrLines=len(Lines)
word=client.Dispatch("Word.Application")
word.Visible=True
Doc=word.Documents.Add()
rang=Doc.Range(Start=0,End=0)
Doc.Tables.Add(rang,NumRows=2,NumColumns=1)
index=2+NrLines
Doc.Tables(1).Rows(2).Cells(1).Split(1,3)
width= Doc.Tables(1).Rows(1).Cells(1).Width
Doc.Tables(1).Rows(1).Cells(1).Range.Bold=True
Doc.Tables(1).Rows(1).Cells(1).Range.Font.Size=15
Doc.Tables(1).Rows(1).Cells(1).Range.Text='Report of LoadFlow Calculations from PowerFactory'
Doc.Tables(1).Cell(2,1).Range.Text='Name of the line'
Doc.Tables(1).Cell(2,2).Range.Text='Loading'
Doc.Tables(1).Cell(2,3).Range.Text='Comment'
app.PrintPlain(Doc.Tables(1).Rows(2).Cells)
for i,line in enumerate(Lines):
    Doc.Tables(1).Rows.Add()
    Doc.Tables(1).Cell(i+3,1).Range.Text=line.loc_name
    Doc.Tables(1).Cell(i+3,2).Range.Text=str(round(line.GetAttribute('c:loading'),2))+ ' %'
    if(line.GetAttribute('c:loading')>60):
        Doc.Tables(1).Cell(i+3,3).Range.Font.Color=225
        Doc.Tables(1).Cell(i+3,3).Range.Text='LoadingOver 60%'

Back