Question

I'd like to fill MSWord bookmarks from a python script. I can't find such functions in win32com(MSWord) or in PyUno(OpenOffice).

Does anyone know how to use bookmarks from Python?

Was it helpful?

Solution

You don't find the functions in win32com, you find them in the documentation for the COM object that you are using. In this case that would be Word.Application.

You can see some sample Python code that uses this COM object to create bookmarks.

The most recent Word Object Model Reference is found here at MSDN

OTHER TIPS

Look at this example for your issue:

def addText(self, bookmark):
    self.wordApp.ActiveDocument.Bookmarks(bookmark).Select()
    self.wordApp.Selection.TypeText(self.some_text)

# from pandas data frame into word table 
def addTable(self, bookmark, df):
    self.wordApp.ActiveDocument.Bookmarks(bookmark).Select()
     table = location.Tables.Add(location, len(df) + 1, len(df.columns), 1, 1)
    table.AutoFormat(40)
    for i, item in enumerate(df):
        table.Cell(1, i + 1).Range.InsertAfter(item)
        table.Cell(1, i + 1).Range.ParagraphFormat.Alignment = 1
    sel.SelectRow()
    sel.BoldRun()
    table.Rows(1).HeadingFormat = True
    for c in range(2, len(df) + 2):
        for r in range(1, len(df.columns) + 1):
            table.Cell(c, r).Range.ParagraphFormat.Alignment = 1
            if pd.isnull(df.ix[c - 2][r - 1]):
                continue
            table.Cell(c, r).Range.InsertAfter(df.ix[c - 2, r - 1])
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top