It's easy to use Python to read and write Excel files. Recently, because Excel is often used, you need to process some apk files based on the content in the Excel table. it is very troublesome to manually process them, so you decided to write scripts for processing. First, paste the Excel-reading and writing scripts found on the Internet.
1. read Excel (xlrd needs to be installed ):
The code is as follows:
#-*-Coding: utf8 -*-
Import xlrd
Fname = "reflect.xls"
Bk = xlrd. open_workbook (fname)
Shxrange = range (bk. nsheets)
Try:
Sh = bk. sheet_by_name ("Sheet1 ")
Except t:
Print "no sheet in % s named Sheet1" % fname
# Obtain the number of rows
Nrows = sh. nrows
# Retrieve the number of columns
Ncols = sh. ncols
Print "nrows % d, ncols % d" % (nrows, ncols)
# Obtain the first column of data in the first row
Cell_value = sh. cell_value (1, 1)
# Print cell_value
Row_list = []
# Retrieving data from each row
For I in range (1, nrows ):
Row_data = sh. row_values (I)
Row_list.append (row_data)
2. write data to Excel (pyExcelerator must be installed)
The code is as follows:
From pyExcelerator import *
W = Workbook () # Create a Workbook
Ws = w. add_sheet ('hey, Hades') # Create a worksheet
Ws. write (, 'bit') # write bit in the first row and one column
Ws. write (, 'huangt') # write huang in two columns in one row
Ws. write (, 'xuany') # write xuan in two rows and one column
Inclusave('mini.xls ') # save
3. here is an example of reading and writing Excel by yourself.
Read some information in reflect.xlsand write it into the mini.xls file.
The code is as follows:
#-*-Coding: utf8 -*-
Import xlrd
From pyExcelerator import *
W = Workbook ()
Ws = w. add_sheet ('sheet1 ')
Fname = "reflect.xls"
Bk = xlrd. open_workbook (fname)
Shxrange = range (bk. nsheets)
Try:
Sh = bk. sheet_by_name ("Sheet1 ")
Except t:
Print "no sheet in % s named Sheet1" % fname
Nrows = sh. nrows
Ncols = sh. ncols
Print "nrows % d, ncols % d" % (nrows, ncols)
Cell_value = sh. cell_value (1, 1)
# Print cell_value
Row_list = []
Mydata = []
For I in range (1, nrows ):
Row_data = sh. row_values (I)
Pkgdatas = row_data [3]. split (',')
# Pkgdatas. split (',')
# Obtain the first two fields of each package
For pkgdata in pkgdatas:
Pkgdata = '.'. join (pkgdata. split ('.') [: 2])
Mydata. append (pkgdata)
# Sort the list
Mydata = list (set (mydata ))
Print mydata
# Convert the list to a string
Mydata = ','. join (mydata)
# Write data to the first column of each row
Ws. write (I, 0, mydata)
Mydata = []
Row_list.append (row_data [3])
# Print row_list
Inclusave('mini.xls ')
4. now I need to obtain the corresponding apk sample from the server based on the md5 value of the apk that meets the specific requirements in the Excel file.:
The code is as follows:
#-*-Coding: utf8 -*-
Import xlrd
Import OS
Import shutil
Fname = "./excelname.xls"
Bk = xlrd. open_workbook (fname)
Shxrange = range (bk. nsheets)
Try:
# Open the sheet 1 worksheet
Sh = bk. sheet_by_name ("Sheet1 ")
Except t:
Print "no sheet in % s named Sheet1" % fname
# Obtain the number of rows
Nrows = sh. nrows
# Retrieve the number of columns
Ncols = sh. ncols
# Print "nrows % d, ncols % d" % (nrows, ncols)
# Obtain the first column of data in the first row
Cell_value = sh. cell_value (1, 1)
# Print cell_value
Row_list = []
# Range (start row, end row)
For I in range (1, nrows ):
Row_data = sh. row_values (I)
If row_data [6] = "HXB ":
Filename = row_data [3] + ". apk"
# Print "% s" % (I, row_data [3], filename)
Filepath = r "./1/" + filename
Print "% s" % (I, row_data [3], filepath)
If OS. path. exists (filepath ):
Shutil. copy (filepath, r "./myapk /")
Okay, that's exactly what python does for Excel! Some more. it's easy.