python,python-docx问题和win32com参考中的MS Word r/w? [英] MS Word r/w in python, Python-docx issue and win32com references?

查看:93
本文介绍了python,python-docx问题和win32com参考中的MS Word r/w?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我正在尝试针对MS Word文件管理(现在编写)使用不同的API.至此,我只需要编写一个简单的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.

最后,我从该站点 Link 使用了非官方的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)和

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:

  • Python and Microsoft Office – Using PyWin32
  • Use Python to parse Microsoft Word documents using PyWin32 Library

这篇关于python,python-docx问题和win32com参考中的MS Word r/w?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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