The python xlrd operating the EXCEL (a)

import xlrd  
# Open the Excel file to read the data 
filename = r'C: \ Users \ Administrator \ Desktop \ services company payroll water table \ TransferSample.xls' 
the Data = xlrd.open_workbook (filename) 
# Gets Book in a worksheet 
# table = data .sheets () [0] # through index order 
# acquired by name = data.sheet_by_name Table # ( 'name') 
Table data.sheet_by_index = (0) 


# line operation 
nrows = table.nrows # effective in obtaining the sheet rows 
table.row (0) # returns a list of all the cells in the row of objects 
table.row_slice (0) # returns a list of all the columns of the cell objects 
table.row_types (0, start_colx = 0, end_colx = None) # returns a list of the data types of all the cells in the row composition 
table.row_values (0, start_colx = 0, end_colx = None) # returns a list of all the data cells in that row composition 
table .row_len (0) # returns the effective column length of the cell 


operation # column 
the effective number of columns ncols = table.ncols # Get list
table.col (0, start_rowx = 0, end_rowx = None) # returns a list of all the columns of the cell objects 
table.col_slice (0, start_rowx = 0, end_rowx = None) # returns all of the column cell object list consisting 
table.col_types (0, start_rowx = 0, end_rowx = None) # returns a list of the data types of all the cells in the column consisting of 
table.col_values (0, start_rowx = 0, end_rowx = None) # this returns a list of all the data cells in that column composition 


# cell operation 
table.cell (0,0) # returns the cell object 
table.cell_type (0,0) # return data type in the cell 
table.cell_value (0,1) # returns the data in the cell 



below with reference to a skilled example:


import xlrd, unittest # xlrd introduced modules and other related 
class Data_Excel (unittest.TestCase): # encapsulated inside the class Data_Excel convenient later using 
    file_addrec = r'C: \ Users \ Administrator \ Desktop \ payroll services company flow table \ date.xlsx ' # define date.xlsx maintained Excel data file path 
    def open_excel (self, file = file_addrec ): # file = file_addrec # attention in class to bring in a certain def Self 
        the try: # inspection documents have not been acquired 
            self.data = xlrd.open_workbook (File) 
            return self.data 
        the except Exception: 
            Print (File) 
            Print ( 'Eero') 
    DEF excel_table_byindex (Self, File = file_addrec, colnameindex = 0, by_index = 'user table'): 
        # reading this Excel encapsulated in excel_table_byindex function required in this case three parameters 1. 2.sheet file name, the number of columns and rows where 
        self.data = xlrd.open_workbook (file) # Excel data acquisition
        self.table = self.data.sheet_by_name (by_index) # sheet_by_name acquisition sheet using sheet page table data objects called user 
        self.colnames = self.table.row_values (colnameindex) # Get the number of rows is first labeled 0 Excel row all data values of the first row 
        self.nrows = self.table.nrows # get all the effective lines 
        list = [] # Excel general idea is to present the data in a string dictionary of the dictionary form as a list element 

        for rownum in Range (. 1, self.nrows): 
            row = self.table.row_values (rownum) # obtain all the data values for each row the number of rows 
            IF row: 
                App = {#} mainly { 'name': 'zhangsan', 'password': 12324.0}, as to how many elements in the dictionary to see how many columns are mainly 
                for I in Range (len (self.colnames)): 
                    App [self.colnames [I]] = row [I] 
                List .append (App) 
        Print (App) 
        return List 
A = Data_Excel ()
b=a.excel_table_byindex()
print(b)
if __name__=="__main__":
    unittest.main()

 

When you open a file and open the sheet tab, try to catch wrong:

filename = r'C: \ Users \ Administrator \ Desktop \ payroll services company flow table \ TransferSample.xlsx ' 
the try: 
    Data xlrd.open_workbook = (filename) 
the except FileNotFoundError AS E: 
    Print (f'Error: {E} \ n-. Please check the file path is correct ')? 

the try: 
    # = Table data.sheet_by_index (. 3) 
    Table data.sheet_by_name = (R & lt' first page ') 
the except IndexError AS E: 
    Print (' Error:. {} \ n-opened sheet fails, check sheet if there is? '. format (e))

  

 


  

Guess you like

Origin www.cnblogs.com/limmzz/p/11006556.html