Python Python to read and write Excel files to read and write Excel files

Python to read and write Excel files

 Reprinted: https: //www.cnblogs.com/goodhacker/archive/2013/04/12/3016183.html

  Recently due to frequent use to Excel, the need for some of the apk be based on the contents of the Excel spreadsheet processing, manual processing a lot of trouble, and decided to write a script to handle. First posted online scripts to read and write Excel recruited.

  1. Read Excel (need to xlrd):  

Copy the code
#. 1 - * - Coding: UTF8 - * - 
 2 Import to xlrd 
 . 3   
 . 4 = fname "reflect.xls" 
 . 5 xlrd.open_workbook BK = (fname) 
 . 6 shxrange = Range (bk.nsheets) 
 . 7 the try: 
 . 8 SH = bk.sheet_by_name ( "Sheet1") 
 . 9 the except: 
10 Print "Sheet NO% S in the named Sheet1" fname% 
. 11 # Get the number of lines 
12 is sh.nrows nrows = 
13 is acquired columns # 
14 ncols sh.ncols = 
15 Print "nrows% D, % D ncols "% (nrows, ncols) 
16 # of the first row and first column of data acquisition 
. 17 CELL_VALUE sh.cell_value = (1,1) 
18 is #Print CELL_VALUE 
. 19   
20 is row_list = [] 
21 is acquired for each row of data # 
22 for i in Range (. 1, nrows):  
23 is ROW_DATA = sh.row_values (I)
24 row_list.append (ROW_DATA)
Copy the code

  2. Write Excel (to be installed pyExcelerator)  

Copy the code
Import from pyExcelerator *. 1 
2 
. 3 W = the Workbook () to create a workbook # 
4 ws = w.add_sheet ( 'Hey, Hades') # Create a work table 
5 ws.write (0,0, 'bit' ) # in a write row 1 'bit 
. 6 ws.write (0,1,' Huang ') # 1 written in two rows Huang 
. 7 ws.write (1,0,' Xuan ') # 2 is written in a row Xuan 
. 8 w.save ( 'mini.xls') # save
Copy the code

  3. As another example of read and write Excel to write their own

  Reflect.xls read certain information written in the file mini.xls after processing.  

Copy the code
 1 #-*- coding: utf8 -*-
 2 import xlrd
 3 from pyExcelerator import *  
 4   
 5 w = Workbook()  
 6 ws = w.add_sheet('Sheet1')  
 7 
 8 fname = "reflect.xls"
 9 bk = xlrd.open_workbook(fname)
10 shxrange = range(bk.nsheets)
11 try:
12     sh = bk.sheet_by_name("Sheet1")
13 except:
14     print "no sheet in %s named Sheet1" % fname
15 
16 nrows = sh.nrows
17 ncols = sh.ncols
18 print "nrows %d, ncols %d" % (nrows,ncols)
19  
20 cell_value = sh.cell_value(1,1)
21 #print cell_value
22  
23 row_list = []
24 mydata = []
For I in Range 25 (. 1, nrows): 
26 is ROW_DATA = sh.row_values (I) 
27 pkgdatas ROW_DATA = [. 3] .split ( ',') 
28 # pkgdatas.split ( ',') 
29 acquires each packet # the first two fields 
30 for pkgdata in pkgdatas: 
31 is the Join pkgdata = ((pkgdata.split ()) '.' [: 2]) '.'. 
32 mydata.append (pkgdata) 
33 is to sort the list # 
34 mydata = list (SET (mydata)) 
35 Print mydata 
36 into a string list # 
37 [mydata = ','. the Join (mydata) 
38 is # writing data to the first column of each row 
39 ws.write (i, 0, mydata) 
40 mydata = [] 
41 is row_list.append (ROW_DATA [. 3]) 
42 is #Print row_list 
43 is 
44 is w.save ( 'mini.xls')
Copy the code

  md5 value apk 4. Now I need to meet specific requirements according to the Excel file to obtain the corresponding apk sample from the server, you need to do:  

Copy the code
# - * - Coding: utf8 - * - 
Import xlrd 
Import os 
Import shutil 
 
fname = "./excelname.xls" 
BK = xlrd.open_workbook (fname) 
shxrange = the Range (bk.nsheets) 
the try: 
    # Open Sheet1 worksheet 
    sh = bk.sheet_by_name ( "Sheet1") 
the except: 
    Print "Sheet NO% S in the named Sheet1" fname% 
# Get the number of rows 
nrows = sh.nrows 
# number of columns in 
ncols = sh.ncols 
#Print "nrows% D, D ncols% "% (nrows, ncols) 
# Get the first row first column data 
CELL_VALUE sh.cell_value = (1,1) 
#Print CELL_VALUE 
 
row_list = [] 
#range (start line, end line) 
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  %s" %(i,row_data[3],filename)
        filepath = r"./1/"+filename
        print "%s  %s  %s" %(i,row_data[3],filepath)
        if os.path.exists(filepath):
            shutil.copy(filepath, r"./myapk/")
Copy the code

Well, python operation Excel so! Some of the simple bar

  Recently due to frequent use to Excel, the need for some of the apk be based on the contents of the Excel spreadsheet processing, manual processing a lot of trouble, and decided to write a script to handle. First posted online scripts to read and write Excel recruited.

  1. Read Excel (need to xlrd):  

Copy the code
#. 1 - * - Coding: UTF8 - * - 
 2 Import to xlrd 
 . 3   
 . 4 = fname "reflect.xls" 
 . 5 xlrd.open_workbook BK = (fname) 
 . 6 shxrange = Range (bk.nsheets) 
 . 7 the try: 
 . 8 SH = bk.sheet_by_name ( "Sheet1") 
 . 9 the except: 
10 Print "Sheet NO% S in the named Sheet1" fname% 
. 11 # Get the number of lines 
12 is sh.nrows nrows = 
13 is acquired columns # 
14 ncols sh.ncols = 
15 Print "nrows% D, % D ncols "% (nrows, ncols) 
16 # of the first row and first column of data acquisition 
. 17 CELL_VALUE sh.cell_value = (1,1) 
18 is #Print CELL_VALUE 
. 19   
20 is row_list = [] 
21 is acquired for each row of data # 
22 for i in Range (. 1, nrows): 
23 is ROW_DATA = sh.row_values (I)
24     row_list.append(row_data)
Copy the code

  2. Write Excel (to be installed pyExcelerator)  

Copy the code
Import from pyExcelerator *. 1 
2 
. 3 W = the Workbook () to create a workbook # 
4 ws = w.add_sheet ( 'Hey, Hades') # Create a work table 
5 ws.write (0,0, 'bit' ) # in a write row 1 'bit 
. 6 ws.write (0,1,' Huang ') # 1 written in two rows Huang 
. 7 ws.write (1,0,' Xuan ') # 2 is written in a row Xuan 
. 8 w.save ( 'mini.xls') # save
Copy the code

  3. As another example of read and write Excel to write their own

  Reflect.xls read certain information written in the file mini.xls after processing.  

Copy the code
 1 #-*- coding: utf8 -*-
 2 import xlrd
 3 from pyExcelerator import *  
 4   
 5 w = Workbook()  
 6 ws = w.add_sheet('Sheet1')  
 7 
 8 fname = "reflect.xls"
 9 bk = xlrd.open_workbook(fname)
10 shxrange = range(bk.nsheets)
11 try:
12     sh = bk.sheet_by_name("Sheet1")
13 except:
14     print "no sheet in %s named Sheet1" % fname
15 
16 nrows = sh.nrows
17 ncols = sh.ncols
18 print "nrows %d, ncols %d" % (nrows,ncols)
19  
20 cell_value = sh.cell_value(1,1)
21 #print cell_value
22  
23 row_list = []
24 mydata = []
For I in Range 25 (. 1, nrows): 
26 is ROW_DATA = sh.row_values (I) 
27 pkgdatas ROW_DATA = [. 3] .split ( ',') 
28 # pkgdatas.split ( ',') 
29 acquires each packet # the first two fields 
30 for pkgdata in pkgdatas: 
31 is the Join pkgdata = ((pkgdata.split ()) '.' [: 2]) '.'. 
32 mydata.append (pkgdata) 
33 is to sort the list # 
34 mydata = list (SET (mydata)) 
35 Print mydata 
36 into a string list # 
37 [mydata = ','. the Join (mydata) 
38 is # writing data to the first column of each row 
39 ws.write (i, 0, mydata) 
40 mydata = [] 
41 is row_list.append (ROW_DATA [. 3]) 
42 is #Print row_list 
43 is 
44 is w.save ( 'mini.xls')
Copy the code

  md5 value apk 4. Now I need to meet specific requirements according to the Excel file to obtain the corresponding apk sample from the server, you need to do:  

Copy the code
# - * - Coding: utf8 - * - 
Import xlrd 
Import os 
Import shutil 
 
fname = "./excelname.xls" 
BK = xlrd.open_workbook (fname) 
shxrange = the Range (bk.nsheets) 
the try: 
    # Open Sheet1 worksheet 
    sh = bk.sheet_by_name ( "Sheet1") 
the except: 
    Print "Sheet NO% S in the named Sheet1" fname% 
# Get the number of rows 
nrows = sh.nrows 
# number of columns in 
ncols = sh.ncols 
#Print "nrows% D, D ncols% "% (nrows, ncols) 
# Get the first row first column data 
CELL_VALUE sh.cell_value = (1,1) 
#Print CELL_VALUE 
 
row_list = [] 
#range (start line, end line) 
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  %s" %(i,row_data[3],filename)
        filepath = r"./1/"+filename
        print "%s  %s  %s" %(i,row_data[3],filepath)
        if os.path.exists(filepath):
            shutil.copy(filepath, r"./myapk/")
Copy the code

Well, python operation Excel so! Some of the simple bar

Guess you like

Origin www.cnblogs.com/yuany66/p/11781022.html