python操做excel(報表比對工具)

先上幾段代碼,這個是用python語言改寫以前的  java POI讀寫excel(項目實際需求) 目的是爲了更好的掌握python,新手請勿唾棄。呵呵html

ReadXlsExceljava

# -*- coding: utf-8 -*-

__author__ = 'Administrator'

import xlrd,time

from CompareExcel import *

from ireadexcel.IReadExcel import IReadExcel

class ReadXlsExcel(IReadExcel):

def __init__(self):

print 'readExcel------init----'

def readExcel(self,reportType='reportType'):

print '2222222'

def open_excel(file = 'file.xls'):

try :

data = xlrd.open_workbook(file)

return data

except Exception ,e:

print str(e)

#根據索引獲取excel表格中的數據 參數:file:excel文件路徑 comnameindex:表頭列名所在行的因此 ,by——inde 表的索引

def excel_table_byindex(file = 'file.xls',colnameindex = 6,by_index =0,p='p'):

data = p.open_excel(file)

table = data.sheets()[by_index]

nrows = table.nrows #行數

ncols = table.ncols #列數

colnames = table.row_values(colnameindex) #某一行的數據

print colnames

list = [] #空的列表

for rownum in range(1,nrows) :

row = table.row_values(rownum)

if row :

app = {} #定義一個空的字典

for i in range(len(colnames)) : #若是你須要一個數值序列,內置函數range()可能會頗有用,它生成一個等差級數鏈表

app[colnames[i]] = row[i]

list.append()

return list

#根據名稱獲取excel表格中的數據   參數file:excel文件路徑 colnameindex:表頭列名所在行的因此,by_name :sheet1名稱

def excel_table_byname(file = 'file.xls',colnameindex = 3,by_name =u'Sheet1',p1='p'):

data = p1.open_excel(file)

table = data.sheet_by_name(by_name)

nrows = table.nrows #行數

#colnames = table.row_values(6) #某一行的數據

list =[]

for rownum in range(1,nrows):

row = table.row_values(rownum)

if row :

app = {}

for i in range(len(row)):

app[i] = row[i]

list.append(app)

#這裏的數據結構相似於在java中的list裏放了不少map,這裏的map爲key值爲每一列的列數,value對應的列對應的值,一行對應一個map

return list

def mai():

#for i in range(1,73):

print time.time()

p2 = ReadXlsExcel()

tables = p2.excel_table_byname(file="e:\\report\\alipay.xls",colnameindex=3,p1=p2)

print time.time()

return tables

if __name__=="__main__":

list = ReadXlsExcel.mai()

com = CompareExcel()

com.compareExcel(alipaylist=list)

CompareExcelpython

# -*- coding: utf-8 -*-
__author__ = 'Administrator'
class CompareExcel:
    def __init__(self):
        print 'init------'
 
    def compareExcel(self,qlylist='listqlyDict',alipaylist ='listalipayDict'):
        '''這裏的算法爲,比較tp的票號,和qly的票號是否相同,若是相同,則比較他們的金額是否相同,而後將比對的結果計入到相同的數據結構中'''
        print 'compare'
        for alipay in alipaylist:
            #alipay業務流水號
            alipay_key = alipay[1]
            for qly in qlylist:
                #qly業務流水號
                qly_key = qly[16]
                print alipay_key
 
            #for q in qly.keys():
相關文章
相關標籤/搜索