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

见程序:

  1 import win32com.client
  2 import os
  3 
  4 
  5 class easyword:
  6     def __init__(self, filename = None):
  7          self.wordapp = win32com.client.gencache.EnsureDispatch("Word.Application")
  8          self.wordapp.Visible = 1
  9          if filename:
 10              self.filename = os.path.abspath(filename)
 11              self.wordapp.Documents.Open(self.filename)
 12 
 13     def Save(self, file_):
 14         if file_:
 15             path = os.path.abspath(file_)
 16             self.wordapp.ActiveDocument.SaveAs(path)
 17         else:
 18             self.wordapp.ActiveDocument.Save()
 19 
 20     def Find(self, findstr):
 21         self.wordapp.Selection.Find.Wrap = 1
 22         self.wordapp.Selection.Find.Execute(findstr)
 23 
 24     def Replace(self, src, dst):
 25         wdFindContinue = 1
 26         wdReplaceAll = 2
 27         find_str = src
 28         replace_str = dst
 29         self.wordapp.Selection.Find.Execute(find_str, False, False, False, False, False, \
 30                 True, wdFindContinue, False, replace_str, wdReplaceAll)
 31 
 32     def close(self):
 33         self.wordapp.ActiveWindow.Close()
 34 
 35     def MoveRight(self, step = 1):
 36         self.wordapp.Selection.MoveRight(1,step)
 37 
 38     def MoveLeft(self, step = 1):
 39         self.wordapp.Selection.MoveLeft(1,step)
 40 
 41     def MoveUp(self):
 42         self.wordapp.Selection.MoveUp()
 43 
 44     def MoveDown(self):
 45         self.wordapp.Selection.MoveDown()
 46 
 47     def HomeKey(self):
 48         self.wordapp.Selection.HomeKey()
 49 
 50     def EndKey(self):
 51         self.wordapp.Selection.EndKey()
 52 
 53     def EscapeKey(self):
 54         self.wordapp.Selection.EscapeKey()
 55 
 56     def Delete(self):
 57         self.wordapp.Selection.Delete ()
 58 
 59     def SelectCell(self):
 60         self.wordapp.Selection.SelectCell()
 61 
 62     def Copy(self):
 63         self.wordapp.Selection.Copy()
 64 
 65     def Paste(self):
 66         self.wordapp.Selection.Paste()
 67 
 68     def TypeText(self,text):
 69         self.wordapp.Selection.TypeText(text)
 70 
 71     def CellRight(self):
 72         self.EndKey()
 73         self.MoveRight()
 74 
 75     def CellLeft(self):
 76         self.HomeKey()
 77         self.MoveLeft()
 78 
 79     def DelThenFillCell(self,text):
 80         self.SelectCell()
 81         self.Delete()
 82         self.TypeText(text)
 83 
 84     def InsertRowBelow(self):
 85         self.wordapp.Selection.InsertRowsBelow()
 86 
 87     def SetTableValue(self, tableid, row, col, value):
 88         tab = self.wordapp.ActiveDocument.Tables(tableid).Cell(row,col).Range.Text = value;
 89 
 90     def GetTableValue(self, tableid, row, col, value):
 91         return self.wordapp.ActiveDocument.Tables(tableid).Cell(row,col).Range.Text
 92 
 93     def GetTableRange(self, tableid, row1, col1, row2, col2):
 94         cell1 = self.wordapp.ActiveDocument.Tables(tableid).Cell(row1,col1)
 95         cell2 = self.wordapp.ActiveDocument.Tables(tableid).Cell(row2,col2)
 96         range = self.wordapp.ActiveDocument.Range(cell1.Range.Start, cell2.Range.Start)
 97         return range
 98 
 99     def GetCellByCotent(self, text):
100         pass
101         #return Range
102 
103     def Close(self):
104         self.wordapp.ActiveWindow.Close()
105         self.wordapp.Quit()
106 
107 
108 
109 if __name__ == "__main__":
110     wd = easyword("1.doc")
111     tab = wd.wordapp.ActiveDocument.Tables(1)
112     #wd.wordapp.ActiveDocument.Range(tab.Cell(1,1).Range.Start,tab.Cell(1,2).Range.End)
113     #wd.MoveRight(5)
114     #wd.SetTableValue(1,1,1,'dfd')
115     range = wd.GetTableRange(1,17,1,17,2)
116     #sel = range.Select()
117     range.Select()
118 
119     #wd.selection.InsertRowsBelow()
120     #wd.wordapp.ActiveDocument.Tables(1).Rows(17).Select
121     #wd.selection.InsertRowsAfter()
122     wd.Find("PDQE")

 

posted on 2012-12-21 15:09  rongyilin  阅读(1850)  评论(0编辑  收藏  举报

导航