[英]MS Word r/w in python, Python-docx issue and win32com references?
Recently I'am experimenting with different API's for MS Word file management (writing for now). 最近我正在尝试使用不同的API进行MS Word文件管理(现在写)。 At this point I need just a simple writing python API. 在这一点上,我只需要一个简单的编写python API。 I tried win32com module which prove to be very robust with lack of examples for python online (very little knowledge of VB and C to be able to translate examples from MSDN). 我尝试了win32com模块,证明它非常强大,缺乏python在线的例子(很少有VB和C的知识能够从MSDN翻译示例)。
I tried to use python-docx but after install I am getting this traceback for any docx function. 我尝试使用python-docx,但在安装后我得到任何docx函数的回溯。
Traceback (most recent call last):
File "C:\filepath.py", line 9, in <module>
ispit = newdocument()
NameError: name 'newdocument' is not defined
I had some problems with installation of lxml by source and by easy_install. 我通过source和easy_install安装lxml时遇到了一些问题。 It was checking for libxlm2 and libxslt binaries. 它正在检查libxlm2和libxslt二进制文件。 I downloaded them and added environmental paths but the installation trough source or easy_install stopped every time. 我下载了它们并添加了环境路径,但每次都停止安装槽源或easy_install。
Finally I used unofficial python extension package from this site Link . 最后我使用了这个站点链接的非官方python扩展包。 Installation was fast and there was no errors in the end. 安装速度很快,最终没有错误。
Is there something I can do to make docx work and is there some python win32com related references online? 我可以做些什么来使docx工作,并且在线有一些python win32com相关参考吗? I couldn't find any. 我找不到任何东西。 (except MSDN (VB not python) and O'Reily's Python programming on win32 ) (除了MSDN (VB不是python)和O'Reily在win32上的Python编程 )
When using win32com
, bear in mind that you are talking to the Word object model. 使用win32com
,请记住您正在与Word对象模型进行通信。 You don't need to know a lot of VBA or other languages to apply the samples to using Python; 您不需要知道很多VBA或其他语言就可以使用Python来应用这些示例; you just need to figure out which parts of the object model are being used. 你只需要弄清楚对象模型的哪些部分正在被使用。
Let's take the following sample (in VBA) which will create a new instance of the Application
, and load a new document into that new instance: 让我们采用以下示例(在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
A similar snippet of code in Python might look like this: 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!"
Some links to the Word object model: 一些指向Word对象模型的链接:
Some Python examples: 一些Python示例:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.