ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/ns_dev/Python/NinoCode/Tool_Box/WordLib.py
Revision: 606
Committed: Wed Jun 15 15:54:16 2016 UTC (9 years, 9 months ago) by nino.borges
Content type: text/x-python
File size: 1932 byte(s)
Log Message:
Created a word automation library to make automating word a bit easier.

File Contents

# User Rev Content
1 nino.borges 606 """
2     WordLib
3    
4     Created by
5     Emanuel Borges
6     12.08.2015
7    
8     Just a Word library to make automating word a bit faster for me.
9    
10     """
11    
12     import win32com.client
13    
14     class WordConnection:
15     def __init__(self,fileName = None):
16     # Dispatch() attempts to do a GetObject() before creating a new one.
17     # DispatchEx() just creates a new one.
18     self.wordApp = win32com.client.DispatchEx("Word.Application")
19     self.wordApp.Visible = 0
20     self.wordApp.DisplayAlerts = 0
21    
22    
23     if fileName:
24     self.fileName = fileName
25     self.wordApp.Documents.Open(self.fileName)
26     else:
27     ## Replace this soon so taht it makes a new if you dont pass filename
28     #self.xlBook = self.xlApp.Workbooks.Add()
29     self.fileName = ''
30    
31     def Save(self, newFileName = None):
32     if newFileName:
33     self.fileName = newFileName
34     self.wordApp.ActiveDocument.SaveAs(newFileName)
35     else:
36     self.wordApp.ActiveDocument.Save()
37    
38     def Close(self):
39     self.wordApp.ActiveDocument.Close()
40     self.wordApp.Quit()
41    
42     def table_row_delete(self,tableNumber,rowNumber):
43     self.wordApp.ActiveDocument.Tables(tableNumber).Rows(rowNumber).Delete()
44    
45     def search_replace_all(self, find_str, replace_str):
46     ''' replace all occurrences of `find_str` w/ `replace_str` in `word_file` '''
47     wdFindContinue = 1
48     wdReplaceAll = 2
49    
50     # expression.Execute(FindText, MatchCase, MatchWholeWord,
51     # MatchWildcards, MatchSoundsLike, MatchAllWordForms, Forward,
52     # Wrap, Format, ReplaceWith, Replace)
53     self.wordApp.Selection.Find.Execute(find_str, False, False, False, False, False, \
54     True, wdFindContinue, False, replace_str, wdReplaceAll)
55    
56    
57     #app.ActiveDocument.Close(SaveChanges=True)
58