簡體   English   中英

python xlrd,從工作簿的最后一張紙中讀取

[英]python xlrd, read from last sheet in a workbook

我正在編寫一個代碼,它應該比較 2 個 xls 文件中的值。 其中一個文件有超過 1 張紙,我總是只需要從最后一張紙中讀取數據。 我真的不知道如何處理這個。 下面是我的代碼:

#! /usr/bin/python
import xlrd #Import the package to read from excel
#start with station report
station_rep = xlrd.open_workbook("/home/fun/data/Station.xls",encoding_override='utf8') #Open the station report.xls
station_sheet = station_rep.sheet_by_index(0) #should get the last sheet
station_vn = station_sheet.col_values(5, start_rowx=1, end_rowx=None) #List of vouchers in station report
#start with billing export
billing_rep = xlrd.open_workbook("/home/fun/data/Export.xls",encoding_override='utf8') #Open billing report xls
billing_sheet = billing_rep.sheet_by_index(0) #get the current sheet 
billing_vn = billing_sheet.col_values(1, start_rowx=0, end_rowx=None)#list of vouchers in billing reports
for vn in station_vn: #For every voucher in station report
    if vn: #if there is data 
        vnb=vn[1:] #change data
        vnb=float(vnb) #change data type to float
        if vnb in billing_vn: # check if voucher exist in billing report
            row=station_vn.index(vn)+1 #take the row of current voucher
            station_vn_data = station_sheet.row_values(row, start_colx=0, end_colx=15) #take the data for current row from station report
            billing_vn_data = billing_sheet.row_values(billing_vn.index(vnb),start_colx=0, end_colx=15) #take the data for current voucher from billing report
            if float(station_vn_data[5])==billing_vn_data[1]: #check if vouchers are equal
                print "nomer na vouchera",  station_vn_data[5], billing_vn_data[1]  
                if round(station_vn_data[10],3)<>round(billing_vn_data[5],3): #check for differences in ammount
                    print "Razlika v edinichna cena", round(station_vn_data[10],3),"-" , round(billing_vn_data[5],3),"=", round(station_vn_data[10]-billing_vn_data[5],3)
                if station_vn_data[11]<>billing_vn_data[4]: #check for difference in price
                    print "kolichestvo", round(station_vn_data[11],4),"-", round(billing_vn_data[4],4),"=",round(station_vn_data[11]-billing_vn_data[4],4) #Ako ima razliki kolichestvata se printirat
                if station_vn_data[12]<>billing_vn_data[6]:# check for 1 more difference
                    print "obshta suma", round(station_vn_data[12],3),"-", round(billing_vn_data[6],3),"=",round(station_vn_data[12]-billing_vn_data[6],3) 
                else:
                    print "voucher is OK"
            print " " #print empty row for more clear view
        else: #if voucher do not exist in billing
            if vnb: 
                print vnb, "does not exist in billing report" #print the voucher number wich don`t exist
station_sheet = station_rep.sheet_by_index(0) #should get the last sheet

沒有理由這應該得到最后一張紙; Python 索引從零開始,因此0是序列中的第一個元素:

>>> [1, 2, 3][0]
1

如果您需要最后一個工作表,請注意 Python 允許從序列末尾開始負索引:

>>> [1, 2, 3][-1]
3

在此基礎上,我認為您想要:

station_sheet = station_rep.sheet_by_index(-1) # get the last sheet
                                         # ^ note index

我設法用該代碼修復它:

for id in  station_rep.sheet_names():
sheet_id=station_rep.sheet_names().index(id)
station_sheet = station_rep.sheet_by_index(sheet_id) #get the last sheet

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM