如何以编程方式使用Microsoft Word的拼写/语法检查器? [英] How can I use Microsoft Word's spelling/grammar checker programmatically?

查看:420
本文介绍了如何以编程方式使用Microsoft Word的拼写/语法检查器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用拼写/语法检查程序处理中到大量的文字片段,以获得粗略的近似值和对其质量的排名。速度也不是真的关注,所以我认为最简单的方法是写一个脚本,将片段传递给Microsoft Word(2007),并对它们运行拼写和语法检查。

I want to process a medium to large number of text snippets using a spelling/grammar checker to get a rough approximation and ranking of their "quality." Speed is not really of concern either, so I think the easiest way is to write a script that passes off the snippets to Microsoft Word (2007) and runs its spelling and grammar checker on them.

有没有办法从一个脚本(具体来说,Python)?

Is there a way to do this from a script (specifically, Python)? What is a good resource for learning about controlling Word programmatically?

如果没有,我想我可以尝试从

If not, I suppose I can try something from Open Source Grammar Checker (SO).

响应Chris的回答,至少有一个方法来打开一个文件(包含代码片段),b)从Word中运行一个VBA脚本,调用拼写和语法检查,和c)返回代码段的得分的一些指示?

In response to Chris' answer, is there at least a way to a) open a file (containing the snippet(s)), b) run a VBA script from inside Word that calls the spelling and grammar checker, and c) return some indication of the "score" of the snippet(s)?

我添加了一个似乎工作的答案,但如果有人有其他建议,我会保持这个问题打开一段时间。

I've added an answer which seems to work, but if anyone has other suggestions I'll keep this question open for some time.

推荐答案

这需要一些挖掘,但我认为我找到了一个有用的解决方案。按照 http://www.nabble.com/的建议Edit-a-Word-document-programmatically-td19974320.html 我正在使用 win32com a>模块,它允许访问Word的COM对象。以下代码很好地演示了这一点:

It took some digging, but I think I found a useful solution. Following the advice at http://www.nabble.com/Edit-a-Word-document-programmatically-td19974320.html I'm using the win32com module, which allows access to Word's COM objects. The following code demonstrates this nicely:

import win32com.client, os

wdDoNotSaveChanges = 0
path = os.path.abspath('snippet.txt')

snippet = 'Jon Skeet lieks ponies.  I can haz reputashunz?  '
snippet += 'This is a correct sentence.'
file = open(path, 'w')
file.write(snippet)
file.close()

app = win32com.client.gencache.EnsureDispatch('Word.Application')
doc = app.Documents.Open(path)
print "Grammar: %d" % (doc.GrammaticalErrors.Count,)
print "Spelling: %d" % (doc.SpellingErrors.Count,)

app.Quit(wdDoNotSaveChanges)

产生


Grammar: 2
Spelling: 3

它与从Word手动调用支票时的结果相同。

which match the results when invoking the check manually from Word.

这篇关于如何以编程方式使用Microsoft Word的拼写/语法检查器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆