MS Word r/w in python、Python-docx 问题和 win32com 引用? [英] MS Word r/w in python, Python-docx issue and win32com references?
问题描述
最近我正在尝试使用不同的 API 来管理 MS Word 文件(暂时写完).在这一点上,我只需要一个简单的编写 python API.我尝试了 win32com 模块,它被证明非常健壮,缺少在线 python 示例(对 VB 和 C 知之甚少,能够从 MSDN 翻译示例).
Recently I'am experimenting with different API's for MS Word file management (writing for now). At this point I need just a simple writing 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).
我尝试使用 python-docx,但安装后我得到了任何 docx 函数的回溯.
I tried to use python-docx but after install I am getting this traceback for any docx function.
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 二进制文件.我下载了它们并添加了环境路径,但安装槽源或 easy_install 每次都停止.
I had some problems with installation of lxml by source and by easy_install. It was checking for libxlm2 and libxslt binaries. I downloaded them and added environmental paths but the installation trough source or easy_install stopped every time.
最后我使用了来自这个站点的非官方 python 扩展包链接.安装很快,最后没有出现错误.
Finally I used unofficial python extension package from this site Link. Installation was fast and there was no errors in the end.
我可以做些什么来使 docx 正常工作,是否有一些与 python win32com 相关的在线参考资料?我找不到任何.(除了 MSDN(VB 不是 python)和 O'Reily 在 win32 上的 Python 编程)
Is there something I can do to make docx work and is there some python win32com related references online? I couldn't find any. (except MSDN(VB not python) and O'Reily's Python programming on win32)
推荐答案
在使用 win32com
时,请记住您是在与 Word 对象模型对话.您不需要了解很多 VBA 或其他语言即可将示例应用到使用 Python 中;你只需要弄清楚对象模型的哪些部分正在被使用.
When using win32com
, bear in mind that you are talking to the Word object model. You don't need to know a lot of VBA or other languages to apply the samples to using Python; you just need to figure out which parts of the object model are being used.
让我们采用以下示例(在 VBA 中),它将创建 Application
的新实例,并将新文档加载到该新实例中:
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:
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 中类似的代码片段可能如下所示:
A similar snippet of code in Python might look like this:
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 对象模型的一些链接:
Some links to the Word object model:
- Application object
- Documents collection
- Document object
- Paragraphs collection
- Paragraph object
- Range object
- Concepts
一些 Python 示例:
Some Python examples:
这篇关于MS Word r/w in python、Python-docx 问题和 win32com 引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!