python中是否有代码可以突出显示Microsoft Word文档中的某些单词(而不是代码语法)?

时间:2017-04-05 00:50:14

标签: python ms-word

我是Python的新手,我正在创建一个代码来搜索和突出显示Word文档中的抽象名词。我已成功编程它以查找.txt文档中的所有抽象名词,但无法找到代码以突出显示其.docx格式的单词。我在研究中可以找到的唯一结果是突出显示代码语法,或者相关搜索结果中的代码输出程序中的错误。例如,任何带有add_run的东西都会返回没有属性的错误。

这是我目前的工作代码:

var str = '"a string"';
str = str.replace(/^"|"$/g, '');

1 个答案:

答案 0 :(得分:0)

你是否使用python-docx库?也许您需要阅读文档http://python-docx.readthedocs.io/en/latest/api/text.html#paragraph-objects

恕我直言最简单的方法使用Word作为COM对象。例如:

msword = win32com.client.Dispatch('Word.Application')
msword.Visible = 0
path = "C:\\temp\\"

try:
    files = os.listdir(path)
    emailfile = open(os.path.join(path, 'emaillist.txt'), 'w')
    compreg = re.compile('([\w\-\.]+@(\w[\w\-]+\.)+[\w\-]+)', re.IGNORECASE)
    for i in range(0, len(files)):
        filename = os.path.join(path, files[i])
        doc = msword.Documents.Open(filename)
       emails = re.findall(p, doc.Content.Text)
       if emails:
            for email in emails: 
                emailfile.write(email)
                emailfile.write()
       print ("[%d] %s DONE" % (i, filename))
       doc = msword.Documents.Close()
except ...
    # process all exceptions here
finally:
    msword.Quit()