ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/ns_dev/Python/NinoCode/Tool_Box/WordLib.py
Revision: 611
Committed: Wed Sep 21 15:10:23 2016 UTC (9 years, 6 months ago) by nino.borges
Content type: text/x-python
File size: 3086 byte(s)
Log Message:
Updated library to now support copying and pasting the contents of an entire word document into another word document.

File Contents

# Content
1 """
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 from win32com.client import constants as c
13 import win32com.client
14
15
16 class WordConnection:
17 def __init__(self,fileName = None):
18 # Dispatch() attempts to do a GetObject() before creating a new one.
19 # DispatchEx() just creates a new one.
20 self.wordApp = win32com.client.DispatchEx("Word.Application")
21 self.wordApp.Visible = 0
22 self.wordApp.DisplayAlerts = 0
23 #wdCollapseEnd = win32com.client.Constants.wdCollapseEnd
24 self.wdCollapseEnd = c.wdCollapseEnd
25
26
27 if fileName:
28 self.fileName = fileName
29 self.wordApp.Documents.Open(self.fileName)
30 else:
31 ## Replace this soon so taht it makes a new if you dont pass filename
32 #self.xlBook = self.xlApp.Workbooks.Add()
33 self.fileName = ''
34
35
36 def Save(self, newFileName = None):
37 if newFileName:
38 self.fileName = newFileName
39 self.wordApp.ActiveDocument.SaveAs(newFileName)
40 else:
41 self.wordApp.ActiveDocument.Save()
42
43 def Close(self):
44 self.wordApp.ActiveDocument.Close()
45 self.wordApp.Quit()
46
47 def table_row_delete(self,tableNumber,rowNumber):
48 self.wordApp.ActiveDocument.Tables(tableNumber).Rows(rowNumber).Delete()
49
50 def search_replace_all(self, find_str, replace_str):
51 ''' replace all occurrences of `find_str` w/ `replace_str` in `word_file` '''
52 wdFindContinue = 1
53 wdReplaceAll = 2
54
55 # expression.Execute(FindText, MatchCase, MatchWholeWord,
56 # MatchWildcards, MatchSoundsLike, MatchAllWordForms, Forward,
57 # Wrap, Format, ReplaceWith, Replace)
58 self.wordApp.Selection.Find.Execute(find_str, False, False, False, False, False, \
59 True, wdFindContinue, False, replace_str, wdReplaceAll)
60
61
62 #app.ActiveDocument.Close(SaveChanges=True)
63
64 def CopyEntireDocument(self):
65 """Copies the entire document into a copy object in memory that can then be pasted in another document"""
66 self.wordApp.ActiveDocument.Select()
67 self.wordApp.Selection.Copy()
68
69 def PasteAtEnd(self):
70 """Takes a copy object and pastes that to the end of this document"""
71
72 range2 = self.wordApp.ActiveDocument.Content
73 ## Move to the end
74 range2.Collapse(self.wdCollapseEnd)
75 ## Paste the copy object in mem to this doc.
76 range2.Paste()
77
78
79 ## Example of using copy and past functions above.
80 ##>>> docA = WordLib.WordConnection(r"C:\Test\TemplateTest\TermsAndConditions.docx")
81 ##>>> docB = WordLib.WordConnection(r"C:\Test\TemplateTest\Forensic Short Form SOW3.docx")
82 ##>>> docA.CopyEntireDocument()
83 ##>>> docB.PasteAtEnd()
84 ##>>> docB.search_replace_all("<<Client>>", "Umberger Zipster")
85 ##>>> docB.Save()
86 ##>>> docB.Close()
87 ##>>> docA.Close()