最近,我正在尝试使用不同的 API 进行 MS Word 文件管理(现在正在编写)。此时我只需要一个简单的编写Python API。我尝试了 win32com 模块,事实证明该模块非常强大,但缺乏 python 在线示例(对 VB 和 C 的了解很少,无法翻译 MSDN 中的示例)。
我尝试使用 python-docx 但安装后我得到了任何 docx 函数的回溯。
Traceback (most recent call last):
File "C:\filepath.py", line 9, in <module>
ispit = newdocument()
NameError: name 'newdocument' is not defined
我在通过源代码和 easy_install 安装 lxml 时遇到了一些问题。它正在检查 libxlm2 和 libxslt 二进制文件。我下载了它们并添加了环境路径,但每次通过 source 或 easy_install 安装都会停止。
最后我使用了这个网站的非官方 python 扩展包Link http://www.lfd.uci.edu/~gohlke/pythonlibs/。
安装速度很快,最后没有出现任何错误。
我可以做些什么来使 docx 工作吗?网上是否有一些与 python win32com 相关的参考资料?我找不到任何。 (除了MSDN http://msdn.microsoft.com/en-us/(VB 不是 python)和O'Reily 在 win32 上的 Python 编程 http://shop.oreilly.com/product/9781565926219.do)
使用时win32com
,请记住您正在与 Word 对象模型对话。您无需了解大量 VBA 或其他语言即可将示例应用到 Python;您只需要弄清楚正在使用对象模型的哪些部分。
让我们看下面的示例(在 VBA 中),它将创建一个新的实例Application
,并将新文档加载到该新实例中:
Public Sub NewWordApp()
'Create variables to reference objects
'(This line is not needed in Python; you don't need to declare variables
'or their types before using them)
Dim wordApp As Word.Application, wordDoc As Word.Document
'Create a new instance of a Word Application object
'(Another difference - in VBA you use Set for objects and simple assignment for
'primitive values. In Python, you use simple assignment for objects as well.)
Set wordApp = New Word.Application
'Show the application
wordApp.Visible = True
'Create a new document in the application
Set wordDoc = wordApp.Documents.Add()
'Set the text of the first paragraph
'(A Paragraph object doesn't have a Text property. Instead, it has a Range property
'which refers to a Range object, which does have a Text property.)
wordDoc.Paragraphs(1).Range.Text = "Hello, World!"
End Sub
Python 中的类似代码片段可能如下所示:
import win32com.client
#Create an instance of Word.Application
wordApp = win32com.client.Dispatch('Word.Application')
#Show the application
wordApp.Visible = True
#Create a new document in the application
wordDoc = wordApp.Documents.Add()
#Set the text of the first paragraph
wordDoc.Paragraphs(1).Range.Text = "Hello, World!"
Word 对象模型的一些链接:
-
应用 http://msdn.microsoft.com/en-us/library/ff838565%28v=office.14%29.aspx object
-
文件 http://msdn.microsoft.com/en-us/library/ff840891%28v=office.14%29.aspx收藏
-
Document http://msdn.microsoft.com/en-us/library/ff822963%28v=office.14%29.aspx object
-
段落 http://msdn.microsoft.com/en-us/library/ff837506%28v=office.14%29.aspx收藏
-
段落 http://msdn.microsoft.com/en-us/library/ff845307%28v=office.14%29.aspx object
-
Range http://msdn.microsoft.com/en-us/library/ff845882%28v=office.14%29.aspx object
- Concepts http://msdn.microsoft.com/en-us/library/ff192359%28v=office.14%29.aspx
一些 Python 示例:
- Python 和 Microsoft Office – 使用 PyWin32 http://www.blog.pythonlibrary.org/2010/07/16/python-and-microsoft-office-using-pywin32/
- 使用 PyWin32 库使用 Python 解析 Microsoft Word 文档 http://www.galalaly.me/index.php/2011/09/use-python-to-parse-microsoft-word-documents-using-pywin32-library/
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)