加载中…
个人资料
麦兜搞IT
麦兜搞IT
  • 博客等级:
  • 博客积分:0
  • 博客访问:1,532,476
  • 关注人气:531
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
相关博文
推荐博文
谁看过这篇博文
加载中…
正文 字体大小:

Python操作Word

(2010-11-07 19:22:22)
标签:

python

word

win32com

it

分类: Python

首先别忘了装win32com的库。

 

#!/usr/bin/env python  

# -*- coding: utf-8 -*-  

import win32com.client

import os

#--------------------------------------------------------------------------

class easyWord:

       '''

       Some convenience methods for Excel documents accessed

       through COM.

       '''

       def __init__(self,visible=False):

              self.wdApp = win32com.client.Dispatch('Word.Application')

              self.wdApp.Visible = visible

      

       def new(self,filename=None):

              '''

              Create a new Word document. If 'filename' specified,

              use the file as a template.

              '''

              if filename:

                     return self.wdApp.Documents.Add(filename)

              else:

                     return self.wdApp.Documents.Add()

      

       def open(self,filename):

              '''

              Open an existing Word document for editing.

              '''

              return self.wdApp.Documents.Open(filename)

             

       def visible(self,visible=True):

              self.wdApp.Visible = visible

      

       def find(self,text,MatchWildcards=False):

              '''

              Find the string

              '''

              find = self.wdApp.Selection.Find

              find.ClearFormatting()

              find.Execute(text, False, False, MatchWildcards, False, False, True, 0)

              return self.wdApp.Selection.Text

             

       def replaceAll(self,oldStr,newStr):

              '''

              Find the oldStr and replace with the newStr.

              '''

              find = self.wdApp.Selection.Find

              find.ClearFormatting()

              find.Replacement.ClearFormatting()

              find.Execute(oldStr, False, False, False, False, False, True, 1, True, newStr, 2)   

      

       def updateToc(self):

              for tocitem in self.wdApp.ActiveDocument.TablesOfContents:

                     tocitem.Update()

      

       def save(self):

              '''

              Save the active document

              '''

              self.wdApp.ActiveDocument.Save()

      

       def saveAs(self,filename,delete_existing=True):

              '''

              Save the active document as a different filename.

              If 'delete_existing' is specified and the file already

              exists, it will be deleted before saving.

              '''

              if delete_existing and os.path.exists(filename):

                     os.remove(filename)

              self.wdApp.ActiveDocument.SaveAs(FileName=filename)

             

       def close(self):

              '''

              Close the active workbook.

              '''

              self.wdApp.ActiveDocument.Close()

             

       def quit(self):

              '''

              Quit Word

              '''

              return self.wdApp.Quit()

我的更多文章:

0

阅读 评论 收藏 转载 喜欢 打印举报/Report
  • 评论加载中,请稍候...
发评论

    发评论

    以上网友发言只代表其个人观点,不代表新浪网的观点或立场。

      

    新浪BLOG意见反馈留言板 电话:4000520066 提示音后按1键(按当地市话标准计费) 欢迎批评指正

    新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 会员注册 | 产品答疑

    新浪公司 版权所有