用Python更新MS-Word(或openoffice)书签

2024-04-18 03:57:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用python脚本填充MSWord书签。 我在win32com(MSWord)或PyUno(OpenOffice)中找不到这样的函数。在

有人知道如何使用Python中的书签吗?在


Tags: 函数脚本win32compyuno书签openofficemsword
2条回答

请看下面这个问题的例子:

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])

你在win32com中找不到函数,你可以在你使用的COM对象的文档中找到它们。在这种情况下文字应用. 在

您可以看到some sample Python code that uses this COM object to create bookmarks

最近的Word Object Model Reference is found here at MSDN

相关问题 更多 >