#!/usr/bin/env python
# -*- coding: utf-8 -*-
from win32com.client import Dispatch
import win32com.client
import threading
import time
import multiprocessing
from retrying import retry
class easyExcel:
"""A utility to make it easier to get at Excel.
Remembering
to save the data is your problem, as is
error handling.
Operates on one workbook at a time."""
@retry(stop_max_delay=10000,wait_fixed=1000) #Workbooks.Open()
可能会抛出
VBA
的
异常,允许尝试多次打开,最大等待
10s
,每次固定等待
1s
def __init__(self, filename=None):
self.xlApp = win32com.client.Dispatch('Excel.Application')
#
打开
EXCEL
应用接口
if filename:
self.filename = filename
self.xlBook = self.xlApp.Workbooks.Open(filename)
#
打开一个
EXCEL
else:
self.xlBook = self.xlApp.Workbooks.Add()
#
新创建一个
EXCEL
self.filename = ''
def save(self, newfilename=None):
if newfilename:
self.filename = newfilename
self.xlBook.SaveAs(newfilename)
else:
self.xlBook.Save()
def close(self):
self.xlBook.Close(SaveChanges=0)
#SAVECHANGES=0
原表格不保存修改
del self.xlApp
#self.xlApp.Quit()
@retry(stop_max_delay=10000,wait_fixed=1000)
def nrows(self, sheet=1):#
获取表格的最大行数
sht = self.xlBook.Worksheets(sheet)
return sht.UsedRange.Rows.Count
@retry(stop_max_delay=10000,wait_fixed=1000)
def ncolumns(self, sheet=1):#
获取表格的最大列数
sht = self.xlBook.Worksheets(sheet)
return sht.UsedRange.Columns.Count
def getCell(self, sheet, row, col):
"Get value of one cell"
sht = self.xlBook.Worksheets(sheet)