[Python development] operation of Excel read

Keywords: Python Excel

The main idea is to refer to the content of this blog and post the address:

http://www.cnblogs.com/zhoujie/p/python18.html

Here's my own code

The demo code for reading excel data is as follows:

 1 def read_excel_demo():
 2     # Open file
 3     workbook = xlrd.open_workbook(r'C:\Users\wxz\Desktop\2018 Annual credit card expense( Kevin).xlsx')
 4     # Get all sheet
 5     print('Print all sheet Table name:',workbook.sheet_names())
 6     sheet1_name = workbook.sheet_names()[0]
 7     sheet2_name = workbook.sheet_names()[1]
 8     # print(sheet1_name)
 9 
10     # according to sheet Index or name get sheet content
11     sheet1 = workbook.sheet_by_index(0)
12     # TODO problem
13     # sheet2 = workbook.sheet_by_name('sheet1_name')
14     # print(sheet2)
15 
16     # sheet Name of, number of rows, number of columns
17     print(sheet1.name, sheet1.nrows, sheet1.ncols)
18 
19     # Get values (arrays) for entire rows and columns
20     rows = sheet1.row_values(0)
21     cols = sheet1.col_values(0)
22     print(rows)
23     print(cols)
24 
25     # Get cell contents
26     print(sheet1.cell(0, 0).value)
27     print(sheet1.cell_value(0, 0))
28     print(sheet1.row(0)[0].value)
29 
30     # Get the data type of cell content ctype :  0 empty,1 string, 2 number, 3 date, 4 boolean, 5 error
31     print(sheet1.cell(0, 0).ctype)
32 
33     # get date
34     if sheet1.cell(1, 3).ctype == 3:
35         datetime = xlrd.xldate_as_datetime(sheet1.cell(1, 3).value, workbook.datemode)
36         print(datetime.date())

The actual calling functions written by myself are as follows:

 1 def read_excel():
 2     workbook = xlrd.open_workbook(r'C:\Users\wxz\Desktop\2018 Annual credit card expense( Kevin).xlsx')
 3     sheet_name = workbook.sheet_names() # Get all sheet Table name
 4     sheet_length = sheet_name.__len__() # Obtain sheet Table length
 5     print('Read out Excel All sheet Table name:')
 6 
 7     sheet_num = 0
 8     for name in sheet_name:
 9         print(str([sheet_num]) + ': ' + name)
10         sheet_num = sheet_num + 1
11 
12     while True:
13         sheet_index = int(input('\n Please enter what needs to be printed sheet Table number:'))
14         if sheet_index < sheet_length:
15             print('\n Printing<' + workbook.sheet_names()[sheet_index] + '>Content of\n')
16 
17             current_sheet = workbook.sheet_by_index(sheet_index)
18             nrows = current_sheet.nrows
19             for n in range(nrows):
20                 print(current_sheet.row_values(n))
21 
22             print('\n Share' + str(nrows) + 'Line, all printed\n')
23             break;
24         else:
25             print('Error message: sheet Table number exceeds the maximum length, please re-enter')
26 
27 if __name__ == '__main__':
28     read_excel()

In addition, I use bat batch file to call py file. Bat code is as follows:

1 @echo off
2 
3 echo starts the ExcelRead program
4 echo\
5 
6 python G:\PycharmProjects\[180817]ExcelOperation\ExcelRead.py
7 
8 pause

The implementation effect is as follows:

The next step is to arrange the read data according to the table (to be continued)

Posted by gofer on Thu, 02 Jan 2020 11:52:20 -0800