0001 import pythoncom 0002 import win32com.server.util 0003 import util 0004 import time 0005 0006 import win32com, sys, string, win32api, traceback 0007 import win32com.client.dynamic 0008 import win32com.client 0009 import pythoncom 0010 from win32com.axcontrol import axcontrol 0011 from pywintypes import Unicode 0012 from win32com import storagecon 0013 from win32com.test.util import CheckClean 0014 0015 import pywintypes 0016 import win32ui 0017 import win32api, os 0018 0019 S_OK = 0 0020 0021 class LockBytes: 0022 _public_methods_ = [ 'ReadAt', 'WriteAt', 'Flush', 'SetSize', 'LockRegion', 'UnlockRegion', 'Stat' ] 0023 _com_interfaces_ = [ pythoncom.IID_ILockBytes ] 0024 0025 def __init__(self, data = ""): 0026 self.data = data 0027 now = pywintypes.Time(time.time()) 0028 self.ctime = now 0029 self.mtime = now 0030 self.atime = now 0031 0032 def ReadAt(self, offset, cb): 0033 print "ReadAt" 0034 result = self.data[offset:offset + cb] 0035 return result 0036 0037 def WriteAt(self, offset, data): 0038 print "WriteAt " +str(offset) 0039 print "len " + str(len(data)) 0040 print "data:" 0041 #print data 0042 if len(self.data) >= offset: 0043 newdata = self.data[0:offset] + data 0044 print len(newdata) 0045 if len(self.data) >= offset + len(data): 0046 newdata = newdata + self.data[offset + len(data):] 0047 print len(newdata) 0048 self.data = newdata 0049 return len(data) 0050 0051 def Flush(self, whatsthis=0): 0052 print "Flush" + str(whatsthis) 0053 fname = os.path.join(win32api.GetTempPath(), "persist.doc") 0054 open(fname, "wb").write(self.data) 0055 return S_OK 0056 0057 def SetSize(self, size): 0058 print "Set Size" + str(size) 0059 if size > len(self.data): 0060 self.data = self.data + "\000" * (size - len(self.data)) 0061 else: 0062 self.data = self.data[0:size] 0063 return S_OK 0064 0065 def LockRegion(self, offset, size, locktype): 0066 print "LockRegion" 0067 pass 0068 0069 def UnlockRegion(self, offset, size, locktype): 0070 print "UnlockRegion" 0071 pass 0072 0073 def Stat(self, statflag): 0074 print "returning Stat " + str(statflag) 0075 return ( 0076 "PyMemBytes", 0077 storagecon.STGTY_LOCKBYTES, 0078 len(self.data), 0079 self.mtime, 0080 self.ctime, 0081 self.atime, 0082 storagecon.STGM_DIRECT|storagecon.STGM_READWRITE|storagecon.STGM_CREATE , 0083 storagecon.STGM_SHARE_EXCLUSIVE, 0084 "{00020905-0000-0000-C000-000000000046}", 0085 0, # statebits ? 0086 0 0087 ) 0088 0089 0090 class OleClientSite: 0091 _public_methods_ = [ 'SaveObject', 'GetMoniker', 'GetContainer', 'ShowObject', 'OnShowWindow', 'RequestNewObjectLayout' ] 0092 _com_interfaces_ = [ axcontrol.IID_IOleClientSite ] 0093 0094 def __init__(self, data = ""): 0095 self.IPersistStorage = None 0096 self.IStorage = None 0097 0098 def SetIPersistStorage(self, IPersistStorage): 0099 self.IPersistStorage = IPersistStorage 0100 0101 def SetIStorage(self, IStorage): 0102 self.IStorage = IStorage 0103 0104 def SaveObject(self): 0105 print "SaveObject" 0106 if self.IPersistStorage != None and self.IStorage != None: 0107 self.IPersistStorage.Save(self.IStorage,1) 0108 self.IStorage.Commit(0) 0109 return S_OK 0110 0111 def GetMoniker(self, dwAssign, dwWhichMoniker): 0112 print "GetMoniker " + str(dwAssign) + " " + str(dwWhichMoniker) 0113 0114 def GetContainer(self): 0115 print "GetContainer" 0116 0117 def ShowObject(self): 0118 print "ShowObject" 0119 0120 def OnShowWindow(self, fShow): 0121 print "ShowObject" + str(fShow) 0122 0123 def RequestNewObjectLayout(self): 0124 print "RequestNewObjectLayout" 0125 0126 0127 def test(): 0128 # create a LockBytes object and 0129 #wrap it as a COM object 0130 # import win32com.server.dispatcher 0131 lbcom = win32com.server.util.wrap(LockBytes(), pythoncom.IID_ILockBytes) #, useDispatcher=win32com.server.dispatcher.DispatcherWin32trace) 0132 0133 # create a structured storage on the ILockBytes object 0134 stcom = pythoncom.StgCreateDocfileOnILockBytes(lbcom, storagecon.STGM_DIRECT| storagecon.STGM_CREATE | storagecon.STGM_READWRITE | storagecon.STGM_SHARE_EXCLUSIVE, 0) 0135 0136 # create our ClientSite 0137 ocs = OleClientSite() 0138 # wrap it as a COM object 0139 ocscom = win32com.server.util.wrap(ocs, axcontrol.IID_IOleClientSite) 0140 0141 # create a Word OLE Document, connect it to our site and our storage 0142 oocom=axcontrol.OleCreate("{00020906-0000-0000-C000-000000000046}", 0143 axcontrol.IID_IOleObject, 0144 0, 0145 (0,), 0146 ocscom, 0147 stcom, 0148 ) 0149 0150 mf=win32ui.GetMainFrame() 0151 hwnd=mf.GetSafeHwnd() 0152 0153 # Set the host and document name 0154 # for unknown reason document name becomes hostname, and document name 0155 # is not set, debugged it, but don't know where the problem is? 0156 oocom.SetHostNames("OTPython", "This is Cool") 0157 0158 # activate the OLE document 0159 oocom.DoVerb( -1, ocscom, 0, hwnd, mf.GetWindowRect()) 0160 0161 # set the hostnames again 0162 oocom.SetHostNames("OTPython2", "ThisisCool2") 0163 0164 # get IDispatch of Word 0165 doc=win32com.client.Dispatch(oocom.QueryInterface(pythoncom.IID_IDispatch)) 0166 0167 # get IPersistStorage of Word 0168 dpcom=oocom.QueryInterface(pythoncom.IID_IPersistStorage) 0169 0170 # let our ClientSite know the interfaces 0171 ocs.SetIPersistStorage(dpcom) 0172 ocs.SetIStorage(stcom) 0173 0174 # use IDispatch to do the Office Word test 0175 # pasted from TestOffice.py 0176 0177 wrange = doc.Range() 0178 for i in range(10): 0179 wrange.InsertAfter("Hello from Python %d\n" % i) 0180 paras = doc.Paragraphs 0181 for i in range(len(paras)): 0182 paras[i]().Font.ColorIndex = i+1 0183 paras[i]().Font.Size = 12 + (4 * i) 0184 # XXX - note that 0185 # for para in paras: 0186 # para().Font... 0187 # doesnt seem to work - no error, just doesnt work 0188 # Should check if it works for VB! 0189 0190 0191 dpcom.Save(stcom, 0) 0192 dpcom.HandsOffStorage() 0193 # oocom.Close(axcontrol.OLECLOSE_NOSAVE) # or OLECLOSE_SAVEIFDIRTY, but it fails??? 0194 0195 #Save the ILockBytes data to "persist2.doc" 0196 lbcom.Flush() 0197 0198 #exiting Winword will automatically update the ILockBytes data 0199 #and flush it to "%TEMP%\persist.doc" 0200 doc.Application.Quit() 0201 0202 if __name__=='__main__': 0203 test() 0204 pythoncom.CoUninitialize() 0205 CheckClean() 0206
Generated by PyXR 0.9.4