#coding:utf-8
from win32com.client import Dispatch
import os
class excel:
val1 = 'Total'
val2 = 'Total+directive'
val3 = 'name'
title = dict()
# 初始化excel应用程序对象,和变量
def __init__(self):
self.xls = Dispatch("Excel.Application")
self.xls.Visible = 1
self.xls.DisplayAlerts = 0 # 后台运行,不显示,不警告
self.list_content = list() #the content of the current worksheet
self.rows = '' #the row number is
self.cols = '' #the col number is
self.find_result = list() #this is the result of fund
self.xlBook = '' #this is the cur workbook
self.filename = '' #the full path of the file
self.sht_name = '' # the name of the current worksheet
self.wb_name = '' #the name of the workbook
# 打开文件或者新建文件(如果不存在的话)
def open_new(self, filename = None):
if os.path.isfile(str(filename)) is True:
self.filename = filename
xlBook_cur = self.xls.Workbooks.Open(filename)
else:
xlBook_cur = self.xls.Workbooks.Add()
self.filename = ''
self.xlBook = xlBook_cur
self.get_cols()
self.get_rows()
#*****************************************************************************
#the function do all with which file depends on xlBook,sht_name
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)
del self.xls
def get_rows(self,sheet = ""):
if sheet == '':
sht = self.xlBook.Worksheets(1)
else:
sht = self.xlBook.Worksheets(sheet)
self.rows = sht.usedrange.rows.count
return sht.usedrange.rows.count
def get_cols(self,sheet = ""):
if sheet == '':
sht = self.xlBook.Worksheets(1)
else:
sht = self.xlBook.Worksheets(sheet)
self.cols = sht.usedrange.columns.count
return sht.usedrange.columns.count
def get_workbooks(self):
for i in self.xls.workbooks:
print(i.name)
if i.fullname == self.filename:
self.wb_name = i.name.split('.')[0]
print(self.wb_name,'iname')
def get_worksheets(self):
for i in self.xls.worksheets:
print(i.name)
def getCell(self, row=1, col=1, sheet=""): # 获取单元格的数据
if sheet == '':
sht = self.xlBook.Worksheets(1)
else:
sht = self.xlBook.Worksheets(sheet)
return sht.Cells(row, col).Value
def setCell(self, row, col, value, sheet = ''): # 设置单元格的数据
if sheet == '':
sht = self.xlBook.Worksheets(1)
else:
sht = self.xlBook.Worksheets(sheet)
sht.Cells(row, col).Value = value
def get_usedrange(self,sheet = None ):
if sheet == None:
sht = self.xlBook.Worksheets(1)
else:
sht = self.xlBook.Worksheets(sheet)
self.list_content = sht.usedrange.value
#print(self.list_content)
return sht.usedrange.address
def getRange(self, sheet, row1, col1, row2, col2): # 获得一块区域的数据,返回为一个二维元组
"return a 2d array (i.e. tuple of tuples)"
sht = self.xlBook.Worksheets(sheet)
return sht.Range(sht.Cells(row1, col1), sht.Cells(row2, col2)).Value
def find_data(self,value):
n = 0
for i in range(self.cols):
for j in range(self.rows):
if self.list_content[i][j] == value:
n += 1
self.find_result.append([i+1,j+1])
else:
return n
def get_title(self):
# print(self.find_data(self.val1)==1)
# print(self.find_data(self.val2)==1)
if self.find_data(self.val1)==1 and self.find_data(self.val2) ==1:
print(self.find_result)
if self.find_result[0][0] == self.find_result[1][0]:
self.title['same'] = 'row'
self.title['row']=self.find_result[0][0]
self.title[self.val1] = self.find_result[0][1]
self.title[self.val2] = self.find_result[1][1]
elif self.find_result[0][1] == self.find_result[1][1]:
self.title['same'] = 'col'
self.title['col']=self.find_result[0][1]
self.title[self.val1] = self.find_result[0][0]
self.title[self.val2] = self.find_result[1][0]
print(self.title)
print('the '+self.title['same']+' is the same: '+str(self.title['row']))
path = 'C:\\Users\\wangcc\\Desktop\\工作簿1.csv'
xlapp = excel()
xlapp.open_new(path)
'''
第一步,获得文件名
每二步,打开文件
第三步,确定标题栏的位置,是同行还是同列,查找类型,字符串(分别代表数据,和数据所对应的关键字)
第四步,读取数据list_to_data,确定数据位置,依据关键字,区分方法
第五步,锁定,目标工作表位置,插入数据位置,byrows,or,bycolumn,依据是关键字
第六步,设置数据的值data_to_list
第七步,要实现行列转换的思想,还要确定匹配度的问题
'''
xlapp.get_usedrange()
print(xlapp.getCell(8,8))
print(xlapp.getCell(9,8))
print(xlapp.getCell(10,8))
#xlapp.find_data('e')
print(45 == 45.0)
#xlapp.open_new()
xlapp.get_workbooks()
xlapp.get_worksheets()
xlapp.get_title()
读取excel数据
猜你喜欢
转载自blog.csdn.net/wang880117/article/details/88047877
今日推荐
周排行