用Python封装几个常用的Word操作

本文介绍了一个名为easyword的Python类,该类提供了一系列方法来实现对Microsoft Word文档的基本操作,如查找替换文本、移动光标位置、表格数据读写等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import win32com.client
 import os
  
  
 class easyword:
     def __init__(self, filename = None):
          self.wordapp = win32com.client.gencache.EnsureDispatch("Word.Application")
          self.wordapp.Visible = 1
          if filename:
              self.filename = os.path.abspath(filename)
              self.wordapp.Documents.Open(self.filename)
  
     def Save(self, file_):
         if file_:
             path = os.path.abspath(file_)
             self.wordapp.ActiveDocument.SaveAs(path)
         else:
             self.wordapp.ActiveDocument.Save()
  
     def Find(self, findstr):
         self.wordapp.Selection.Find.Wrap = 1
         self.wordapp.Selection.Find.Execute(findstr)
  
     def Replace(self, src, dst):
         wdFindContinue = 1
         wdReplaceAll = 2
         find_str = src
         replace_str = dst
         self.wordapp.Selection.Find.Execute(find_str, False, False, False, False, False, \
                 True, wdFindContinue, False, replace_str, wdReplaceAll)
  
     def close(self):
         self.wordapp.ActiveWindow.Close()
  
     def MoveRight(self, step = 1):
         self.wordapp.Selection.MoveRight(1,step)
  
     def MoveLeft(self, step = 1):
         self.wordapp.Selection.MoveLeft(1,step)
  
     def MoveUp(self):
         self.wordapp.Selection.MoveUp()
  
     def MoveDown(self):
         self.wordapp.Selection.MoveDown()
  
     def HomeKey(self):
         self.wordapp.Selection.HomeKey()
  
     def EndKey(self):
         self.wordapp.Selection.EndKey()
  
     def EscapeKey(self):
         self.wordapp.Selection.EscapeKey()
  
     def Delete(self):
         self.wordapp.Selection.Delete ()
  
     def SelectCell(self):
         self.wordapp.Selection.SelectCell()
  
     def Copy(self):
         self.wordapp.Selection.Copy()
  
     def Paste(self):
         self.wordapp.Selection.Paste()
  
     def TypeText(self,text):
         self.wordapp.Selection.TypeText(text)
  
     def CellRight(self):
         self.EndKey()
         self.MoveRight()
  
     def CellLeft(self):
         self.HomeKey()
         self.MoveLeft()
  
     def DelThenFillCell(self,text):
         self.SelectCell()
         self.Delete()
         self.TypeText(text)
  
     def InsertRowBelow(self):
         self.wordapp.Selection.InsertRowsBelow()
  
     def SetTableValue(self, tableid, row, col, value):
         tab = self.wordapp.ActiveDocument.Tables(tableid).Cell(row,col).Range.Text = value;
  
     def GetTableValue(self, tableid, row, col, value):
         return self.wordapp.ActiveDocument.Tables(tableid).Cell(row,col).Range.Text
  
     def GetTableRange(self, tableid, row1, col1, row2, col2):
         cell1 = self.wordapp.ActiveDocument.Tables(tableid).Cell(row1,col1)
         cell2 = self.wordapp.ActiveDocument.Tables(tableid).Cell(row2,col2)
         range = self.wordapp.ActiveDocument.Range(cell1.Range.Start, cell2.Range.Start)
         return range
  
     def GetCellByCotent(self, text):
         pass
         #return Range
  
     def Close(self):
         self.wordapp.ActiveWindow.Close()
         self.wordapp.Quit()
  
  
  
 if __name__ == "__main__":
     wd = easyword("1.doc")
     tab = wd.wordapp.ActiveDocument.Tables(1)
     #wd.wordapp.ActiveDocument.Range(tab.Cell(1,1).Range.Start,tab.Cell(1,2).Range.End)
     #wd.MoveRight(5)
     #wd.SetTableValue(1,1,1,'dfd')
     range = wd.GetTableRange(1,17,1,17,2)
     #sel = range.Select()
     range.Select()
  
     #wd.selection.InsertRowsBelow()
     #wd.wordapp.ActiveDocument.Tables(1).Rows(17).Select
     #wd.selection.InsertRowsAfter()
     wd.Find("PDQE")


转载于:https://www.cnblogs.com/findeasy/archive/2013/01/02/4053118.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值