Recently because of the frequent use of Excel, you need to follow the contents of the Excel table for some apk processing, manual processing is cumbersome, and decided to write a script to deal with. First post a script that reads and writes Excel online.
1. Read Excel (need to install XLRD):
#-*-Coding:utf8-*-
import xlrd
fname = "Reflect.xls"
bk = Xlrd.open_workbook (fname)
Shxrange = Range ( Bk.nsheets)
try:
sh = bk.sheet_by_name ("Sheet1")
except:
print "No sheet in%s named Sheet1"% FNAME
#获取行数
nrows = sh.nrows
#获取列数
ncols = sh.ncols
print "nrows%d, ncols%d"% (nrows,ncols)
# Get first row of first column data
cell_value = Sh.cell_value (1,1)
#print cell_value
row_list = [] #获取各行数据 for
i in Range (1,nrows):
row_data = sh.row_values (i)
row_list.append (Row_data)
2. Write to Excel (need to install pyexcelerator)
From pyexcelerator import *
w = workbook () #创建一个工作簿
ws = W.add_sheet (' Hey, Hades ') #创建一个工作表
Ws.write (0,0, ' bit ') #在1行1列写入bit
ws.write (0,1, ' Huang ') #在1行2列写入huang
ws.write (1,0, ' Xuan ') #在2行1列写入xuan
w.save (' Mini.xls ') #保存
3. Give me another example of writing and reading Excel.
read some of the information in the Reflect.xls for processing and writing to the Mini.xls file.
#-*-Coding:utf8-*-import xlrd from pyexcelerator import * w = workbook () ws = W.ad D_sheet (' Sheet1 ') fname = "Reflect.xls" BK = Xlrd.open_workbook (fname) Shxrange = Range (bk.nsheets) Try:sh = bk.sheet_ By_name ("Sheet1") except: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 (', ') #获取每个包的前两个字段 for Pkgdata In Pkgdatas:pkgdata = '. '. Join ((Pkgdata.split ('. '))
[: 2]) Mydata.append (pkgdata) #将列表排序 MyData = List (set (MyData)) Print MyData #将列表转化为字符串 MyData = ', '. Join (MyData) #写入数据到每行的 First column ws.write (i,0,mydata) MyData = [] Row_list.append (row_data[3]) #print row_list w.save (' Mini.xls ')
4. Now I need to get the appropriate APK sample from the server based on the MD5 value of the APK in the Excel file that meets the specific requirements :
#-*-coding:utf8-*-
Import xlrd
import os
import shutil
fname = "./excelname.xls"
bk = Xlrd.open_ Workbook (fname)
Shxrange = Range (bk.nsheets)
try:
#打开Sheet1工作表
sh = bk.sheet_by_name ("Sheet1")
except:
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 = []
#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%s"% (I,row_data[3],filename)
filepath = r "./1/" +filename
print "%s%s"% (I,row_data[3],filepath)
if Os.path.exists (filepath):
shutil.copy (filepath, r "./myapk/")
Add a write operation that uses XLWT3 for Excel files.
Import Xlwt3
If __name__ = = ' __main__ ':
datas = [[' A ', ' B ', ' C '], [' d ', ' e ', ' f '], [' G ', ' H ']] #二维数组
file_path = ' d:\\test.xlsx '
wb = xlwt3. Workbook ()
sheet = wb.add_sheet (' Test ') #sheet的名称为test
#单元格的格式
style = ' Pattern:pattern solid, fore_ Colour yellow; ' #背景颜色为黄色
style + = ' font:bold on; ' #粗体字
style + = ' Align:horz centre, vert Center; ' #居中
Header_style = XLWT3.EASYXF (style)
row_count = Len (datas)
col_count = Len (datas[0]) for
row In range (0, Row_count):
col_count = Len (Datas[row]) for
Col in range (0, col_count):
if row = = 0: #设置表头单元格的格 Type
sheet.write (row, col, Datas[row][col], Header_style)
else:
sheet.write (Row, col, Datas[row][col))
Wb.save (File_path)
The contents of the output file are shown below:
Note: The above code is passed in the Python 3.x version test.
Okay, Python operates Excel just like that! Some, it's easy.