1
|
1 |
# Copyright (C) 2005, Giovanni Bajo
|
|
2 |
# Based on previous work under copyright (c) 1999, 2002 McMillan Enterprises, Inc.
|
|
3 |
#
|
|
4 |
# This program is free software; you can redistribute it and/or
|
|
5 |
# modify it under the terms of the GNU General Public License
|
|
6 |
# as published by the Free Software Foundation; either version 2
|
|
7 |
# of the License, or (at your option) any later version.
|
|
8 |
#
|
|
9 |
# This program is distributed in the hope that it will be useful,
|
|
10 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
12 |
# GNU General Public License for more details.
|
|
13 |
#
|
|
14 |
# You should have received a copy of the GNU General Public License
|
|
15 |
# along with this program; if not, write to the Free Software
|
|
16 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
17 |
|
|
18 |
|
|
19 |
# Test MSOffice
|
|
20 |
#
|
|
21 |
# Main purpose of test is to ensure that Dynamic COM objects
|
|
22 |
# work as expected.
|
|
23 |
|
|
24 |
# Assumes Word and Excel installed on your machine.
|
|
25 |
|
|
26 |
import win32com, sys, string, win32api, traceback
|
|
27 |
import win32com.client.dynamic
|
|
28 |
from win32com.test.util import CheckClean
|
|
29 |
import pythoncom
|
|
30 |
from win32com.client import gencache
|
|
31 |
from pywintypes import Unicode
|
|
32 |
|
|
33 |
error = "MSOffice test error"
|
|
34 |
|
|
35 |
# Test a few of the MSOffice components.
|
|
36 |
def TestWord():
|
|
37 |
# Try and load the object exposed by Word 8
|
|
38 |
# Office 97 - _totally_ different object model!
|
|
39 |
try:
|
|
40 |
# NOTE - using "client.Dispatch" would return an msword8.py instance!
|
|
41 |
print "Starting Word 8 for dynamic test"
|
|
42 |
word = win32com.client.dynamic.Dispatch("Word.Application")
|
|
43 |
TestWord8(word)
|
|
44 |
|
|
45 |
word = None
|
|
46 |
# Now we will test Dispatch without the new "lazy" capabilities
|
|
47 |
print "Starting Word 8 for non-lazy dynamic test"
|
|
48 |
dispatch = win32com.client.dynamic._GetGoodDispatch("Word.Application")
|
|
49 |
typeinfo = dispatch.GetTypeInfo()
|
|
50 |
attr = typeinfo.GetTypeAttr()
|
|
51 |
olerepr = win32com.client.build.DispatchItem(typeinfo, attr, None, 0)
|
|
52 |
word = win32com.client.dynamic.CDispatch(dispatch, olerepr)
|
|
53 |
dispatch = typeinfo = attr = olerepr = None
|
|
54 |
TestWord8(word)
|
|
55 |
|
|
56 |
except pythoncom.com_error:
|
|
57 |
print "Starting Word 7 for dynamic test"
|
|
58 |
word = win32com.client.Dispatch("Word.Basic")
|
|
59 |
TestWord7(word)
|
|
60 |
|
|
61 |
try:
|
|
62 |
print "Starting MSWord for generated test"
|
|
63 |
# Typelib, lcid, major and minor for the typelib
|
|
64 |
try:
|
|
65 |
o = gencache.EnsureModule("{00020905-0000-0000-C000-000000000046}", 1033, 8, 0, bForDemand=1)
|
|
66 |
except TypeError:
|
|
67 |
o = gencache.EnsureModule("{00020905-0000-0000-C000-000000000046}", 1033, 8, 0)
|
|
68 |
if o is None :
|
|
69 |
raise ImportError, "Can not load the Word8 typelibrary."
|
|
70 |
word = win32com.client.Dispatch("Word.Application.8")
|
|
71 |
TestWord8(word)
|
|
72 |
except ImportError, details:
|
|
73 |
print "Can not test MSWord8 -", details
|
|
74 |
|
|
75 |
def TestWord7(word):
|
|
76 |
word.FileNew()
|
|
77 |
# If not shown, show the app.
|
|
78 |
if not word.AppShow(): word._proc_("AppShow")
|
|
79 |
|
|
80 |
for i in xrange(12):
|
|
81 |
word.FormatFont(Color=i+1, Points=i+12)
|
|
82 |
word.Insert("Hello from Python %d\n" % i)
|
|
83 |
|
|
84 |
word.FileClose(2)
|
|
85 |
|
|
86 |
def TestWord8(word):
|
|
87 |
word.Visible = 1
|
|
88 |
doc = word.Documents.Add()
|
|
89 |
wrange = doc.Range()
|
|
90 |
for i in range(10):
|
|
91 |
wrange.InsertAfter("Hello from Python %d\n" % i)
|
|
92 |
paras = doc.Paragraphs
|
|
93 |
for i in range(len(paras)):
|
|
94 |
paras[i]().Font.ColorIndex = i+1
|
|
95 |
paras[i]().Font.Size = 12 + (4 * i)
|
|
96 |
# XXX - note that
|
|
97 |
# for para in paras:
|
|
98 |
# para().Font...
|
|
99 |
# doesnt seem to work - no error, just doesnt work
|
|
100 |
# Should check if it works for VB!
|
|
101 |
doc.Close(SaveChanges = 0)
|
|
102 |
word.Quit()
|
|
103 |
win32api.Sleep(1000) # Wait for word to close, else we
|
|
104 |
# may get OA error.
|
|
105 |
|
|
106 |
def TestWord8OldStyle():
|
|
107 |
try:
|
|
108 |
import win32com.test.Generated4Test.msword8
|
|
109 |
except ImportError:
|
|
110 |
print "Can not do old style test"
|
|
111 |
|
|
112 |
|
|
113 |
def TextExcel(xl):
|
|
114 |
xl.Visible = 0
|
|
115 |
if xl.Visible: raise error, "Visible property is true."
|
|
116 |
xl.Visible = 1
|
|
117 |
if not xl.Visible: raise error, "Visible property not true."
|
|
118 |
|
|
119 |
if int(xl.Version[0])>=8:
|
|
120 |
xl.Workbooks.Add()
|
|
121 |
else:
|
|
122 |
xl.Workbooks().Add()
|
|
123 |
|
|
124 |
|
|
125 |
xl.Range("A1:C1").Value = (1,2,3)
|
|
126 |
xl.Range("A2:C2").Value = ('x','y','z')
|
|
127 |
xl.Range("A3:C3").Value = ('3','2','1')
|
|
128 |
|
|
129 |
for i in xrange(20):
|
|
130 |
xl.Cells(i+1,i+1).Value = "Hi %d" % i
|
|
131 |
|
|
132 |
if xl.Range("A1").Value <> "Hi 0":
|
|
133 |
raise error, "Single cell range failed"
|
|
134 |
|
|
135 |
if xl.Range("A1:B1").Value <> ((Unicode("Hi 0"),2),):
|
|
136 |
raise error, "flat-horizontal cell range failed"
|
|
137 |
|
|
138 |
if xl.Range("A1:A2").Value <> ((Unicode("Hi 0"),),(Unicode("x"),)):
|
|
139 |
raise error, "flat-vertical cell range failed"
|
|
140 |
|
|
141 |
if xl.Range("A1:C3").Value <> ((Unicode("Hi 0"),2,3),(Unicode("x"),Unicode("Hi 1"),Unicode("z")),(3,2,Unicode("Hi 2"))):
|
|
142 |
raise error, "square cell range failed"
|
|
143 |
|
|
144 |
xl.Range("A1:C3").Value =((3,2,1),("x","y","z"),(1,2,3))
|
|
145 |
|
|
146 |
if xl.Range("A1:C3").Value <> ((3,2,1),(Unicode("x"),Unicode("y"),Unicode("z")),(1,2,3)):
|
|
147 |
raise error, "Range was not what I set it to!"
|
|
148 |
|
|
149 |
# test dates out with Excel
|
|
150 |
xl.Cells(5,1).Value = "Excel time"
|
|
151 |
xl.Cells(5,2).Formula = "=Now()"
|
|
152 |
|
|
153 |
import time
|
|
154 |
xl.Cells(6,1).Value = "Python time"
|
|
155 |
xl.Cells(6,2).Value = pythoncom.MakeTime(time.time())
|
|
156 |
xl.Cells(6,2).NumberFormat = "d/mm/yy h:mm"
|
|
157 |
xl.Columns("A:B").EntireColumn.AutoFit()
|
|
158 |
|
|
159 |
xl.Workbooks(1).Close(0)
|
|
160 |
xl.Quit()
|
|
161 |
|
|
162 |
def TestAll():
|
|
163 |
try:
|
|
164 |
TestWord()
|
|
165 |
|
|
166 |
print "Starting Excel for Dynamic test..."
|
|
167 |
xl = win32com.client.dynamic.Dispatch("Excel.Application")
|
|
168 |
TextExcel(xl)
|
|
169 |
|
|
170 |
try:
|
|
171 |
print "Starting Excel 8 for generated excel8.py test..."
|
|
172 |
try:
|
|
173 |
mod = gencache.EnsureModule("{00020813-0000-0000-C000-000000000046}", 0, 1, 2, bForDemand=1)
|
|
174 |
except TypeError:
|
|
175 |
mod = gencache.EnsureModule("{00020813-0000-0000-C000-000000000046}", 0, 1, 2)
|
|
176 |
xl = win32com.client.Dispatch("Excel.Application")
|
|
177 |
TextExcel(xl)
|
|
178 |
except ImportError:
|
|
179 |
print "Could not import the generated Excel 97 wrapper"
|
|
180 |
|
|
181 |
try:
|
|
182 |
import xl5en32
|
|
183 |
mod = gencache.EnsureModule("{00020813-0000-0000-C000-000000000046}", 9, 1, 0)
|
|
184 |
xl = win32com.client.Dispatch("Excel.Application.5")
|
|
185 |
print "Starting Excel 95 for makepy test..."
|
|
186 |
TextExcel(xl)
|
|
187 |
except ImportError:
|
|
188 |
print "Could not import the generated Excel 95 wrapper"
|
|
189 |
|
|
190 |
except KeyboardInterrupt:
|
|
191 |
print "*** Interrupted MSOffice test ***"
|
|
192 |
except:
|
|
193 |
traceback.print_exc()
|
|
194 |
|
|
195 |
if __name__=='__main__':
|
|
196 |
TestAll()
|
|
197 |
CheckClean()
|
|
198 |
pythoncom.CoUninitialize()
|
|
199 |
|