1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
|
import win32com.client import pywintypes import pythoncom import os
def GoodString(value): """ >>> GoodString(2) '2' """ try: return str(value) except UnicodeEncodeError: return value
class Lxsoft(): def __init__(self, log, path=''): self.log = log self.lx = self.__registerLxsoft() self.InitExcel() if path: self.Open(path) self.SheetIndex() self.SheetCount()
def __registerLxsoft(self): '''注册插件 ''' pythoncom.CoInitialize() command = "regsvr32 /s atl.dll" os.system(command) try: lx = win32com.client.Dispatch('Lazy.LxjExcel') except pywintypes.com_error: command = 'regsvr32 /s LazyOffice.dll' if os.system(command) == 0: lx = win32com.client.Dispatch('Lazy.LxjExcel') return lx else: self.log.error('regsver32 error') else: return lx def InitExcel(self): self.path = '' self.maxsheet = 1 self.index = 1 self.sindex = 1
def __del__(self): self.Close()
def __Trueindex(self, index): if index == '': return self.index else: return index
def Open(self, path, visual=0): self.path = path self.index = self.lx.ExcelOpen(path, visual) return self.index
def Close(self, index=''): index = self.__Trueindex(index) return self.lx.ExcelClose(index)
def SheetIndex(self, index=''): index = self.__Trueindex(index) self.sindex = self.lx.SheetIndex(index) return self.sindex
def SheetName(self, Sindex, index=''): index = self.__Trueindex(index) return self.lx.SheetGetName(Sindex, index)
def SheetCount(self, index=''): index = self.__Trueindex(index) self.maxsheet = self.lx.SheetCount(index) return self.maxsheet
def SheetAdd(self, Sindex, index=''): index = self.__Trueindex(index) self.log.debug(u'新建标签: '+GoodString(Sindex)) return self.lx.SheetAdd(Sindex, index)
def SheetRename(self, Sindex, name, index=''): index = self.__Trueindex(index) return self.lx.SheetRename(Sindex, name, index)
def SheetDel(self, Sindex, index=''): index = self.__Trueindex(index) self.log.debug(u'删除标签: '+GoodString(Sindex)) return self.lx.SheetDel(Sindex, index)
def Write(self, Sindex, x, y, string, index=''): index = self.__Trueindex(index) self.log.debug(u'向单元格(%s,%s)写入内容: %s' % (GoodString(x),GoodString(y),GoodString(string))) return self.lx.ExcelWrite(Sindex, x, y, string, index)
def Read(self, Sindex, x, y, index=''): index = self.__Trueindex(index) string = self.lx.ExcelRead(Sindex, x, y, index)[0] self.log.debug(u'读取单元格(%s,%s): %s' % (GoodString(x),GoodString(y),GoodString(string))) return string
def differ(self, Sindex, x1, y1, x2, y2, index1='', index2=''): index1 = self.__Trueindex(index1) index2 = self.__Trueindex(index2) return int(self.Read(Sindex, x1, y1, index1)) - int(self.Read(Sindex, x2, y2, index2))
def Cells(self, tab1, action, tab2, index=''): index = self.__Trueindex(index) self.log.debug(u'表%s %s -> %s' % (GoodString(tab1),action,GoodString(tab2))) return self.lx.ExcelCells(tab1, action, tab2, index)
def Rows(self, tab, row1, action, row2, index=''): index = self.__Trueindex(index) self.log.debug(u'行%s %s -> %s' % (GoodString(row1),action,GoodString(row2))) return self.lx.ExcelRows(tab, row1, action, row2, index)
def Columns(self, tab, col1, action, col2, index=''): index = self.__Trueindex(index) self.log.debug(u'列%s %s -> %s' % (GoodString(col1),action,GoodString(col2))) return self.lx.ExcelColumns(tab, col1, action, col2, index)
def Range(self, tab, ranges, action, destination, index=''): index = self.__Trueindex(index) self.log.debug(u'区域%s %s -> %s' % (GoodString(tab),action,GoodString(destination))) return self.lx.ExcelRange(tab, ranges, action, destination, index)
if __name__ == '__main__': import log import time path = 'F:\\test.xls' mylog = log.Log() lx = Lxsoft(mylog, path) a = lx.SheetCount() print(lx.Columns(a, 7, u"模糊查找", "20150722")[0][0]) lx.Close()
|