Importxlrd#01. Open the Excel file and create a file data Objectdata = Xlrd.open_workbook ('/home/python/desktop/excel/test1.xls')#print (data)#02. Get a table#table = data.sheets () [0] # 1. Get by index#table = data.sheet_by_index (0) # 2. Getting an index from a functionTable = Data.sheet_by_name ('Sheet1')#3. Get through the table name#Print (table)#03. Get the value of a row or column (array)#Print (table.row_values (0, Start_colx=1, end_colx=3)) # Get row, limit column#Row_values () consists of 3 parameters:#1. The first few lines, starting from 0;#2. First column: start_colx=0;#3. Tail column: End_colx=none; (left close right open)#>>> [1.0, ' Great god ', ' naked man ', ' dog stuff ']#>>> [' Great god ', ' naked Man ']#Print (table.col_values (0, Start_rowx=1, end_rowx=5)) # Get columns, limit rows#>>> [' Great god ', ' naked Man ']#>>> [2.0, 3.0, 4.0, 5.0]#04. Get the number of rows and columns#print (table.nrows)#print (table.ncols)
ImportXLWT#1. Create an Excel fileTest_excel = XLWT. Workbook (encoding="ASCII")#2. Create a tableTest_sheet = Test_excel.add_sheet ('sheet01')#3. Write data to the tableTest_sheet.write (0, 0, label="write a little something.")#specify that cells write data#4. Save the Excel fileTest_excel.save ('/home/python/desktop/excel/test3.xls')
fromOpenpyxlImportLoad_workbook#1. Reading files#① introducing files, creating objects#WB = Load_workbook ("/home/python/desktop/excel/test2.xlsx")#print (WB)#Print Table name#print (wb.sheetnames)#② getting the specified table#sheet = wb.get_sheet_by_name ("Sheet2")#Print (sheet)#gets the specified location content object#print (sheet["B"]) # Column B object#Print (sheet[' 2 ']) # line 2nd Object#print (sheet[' B2 '].value) # Displays the specified cell information#print (sheet.max_row) # Displays the maximum number of rows that exist#print (sheet.max_column) # Displays the maximum number of columns that exist#Traverse#For i in sheet[' B ']:#print (I.value, end= "") # The data that was originally obtained is a space#2. Writing Files fromOpenpyxlImportWorkbook#To create a file object#WB = Workbook ()## Create an object for the current sheet#sheet_obj = wb.active#print (sheet_obj)## Rename the current worksheet#sheet_obj.title = "Mysheet"#Add Table | The added table is already active#sheet_obj1 = Wb.create_sheet (' Ursheet ')#print (SHEET_OBJ1)#Delete a table#...## Write data to the table#sheet_obj1[' B2 ' = "Hello World"#For I in Range (Ten):#sheet_obj1["a%d"% (i+1)].value = i+1 # Adds data to the specified number of rows in column A## Functions in Excel can be manipulated in Python with a string written#sheet_obj1["C1"].value = "=sum (a:a)"#Save table Data#wb.save ("test4.xlsx")
Python Operations Excel Packages | xlrd | XLXT | Openpyxl