| 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. |
| 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: |
| 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: |
| 60 |
|
|
| 61 |
|
|
| 62 |
|
#app.ActiveDocument.Close(SaveChanges=True) |
| 63 |
< |
|
| 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() |