Python operations Excel uses XLRD, XLWT, and xlutils modules. XLRD module is read in Excel, XLWT module is written in Excel, Xlutils is used to modify Excel's
One, Python read Excel
Import xlrd
Book = Xlrd.open_workbook (' All_stu.xls ')#Open aExcel
Sheet = Book.sheet_by_index (0)#Get By orderSheet
# Sheet2 = Book.sheet_by_name (' Sheet1 ') #UnderSheetPage name getSheet
# Print (Sheet.cell (0,0). Value) #Specifying rows and columns for data
# print (sheet.ncols) #GetExcelHow many columns are there?
# print (sheet.nrows) #GetExcelHow many lines are inside?
Sheet.row_values (1) # Take the first few rows of data
Span style= "color: #808080; Font-style:italic "># Print (Sheet.col_values (1)) # Take the first few columns of data
Span style= "color: #000080; Font-weight:bold ">for i in range ( sheet.nrows): # Get excel How many lines
print (Sheet.row_values (i)) Span style= "color: #808080; Font-style:italic "># Take the first few rows of data
Second, Python operations Excel
Import XLWT
Stus = [
[‘Name‘,‘Age‘,‘Gender‘,‘Scores‘],
[' Mary ',20,‘Woman‘,89.9],
[' Mary ',20,‘Woman‘,89.9],
[' Mary ',20,‘Woman‘,89.9],
[' Mary ',20,‘Woman‘,89.9]
]
Book = XLWT. Workbook ()#Create a newExcel
Sheet = Book.add_sheet (' Sheet1 ')#Add aSheetPage
Sheet.write (0,0,‘Name‘)#Write a line line
# sheet.write (0,1, 'Gender‘)
# sheet.write (0,2, 'Age‘)
# book.save (' Stu.xls ') #Microsoft'sOfficeCan't usexlsxEnd OF,WpsRandom
Raw =0#of the control line
For StuIn Stus:#Looping writes
Col =0# control columns, which do not loop once, are written from the first column to
for s in stu:
Sheet.write (raw,col,s)
Col+=1
Raw+=1
Book.save (" Kkk.xls ') # Microsoft's office not Available xlsx end, Span style= "color: #808080; Font-style:italic ">wps casual
Third, Python modifies Excel
From Xlutils.copyImport Copy
Import xlrd
Book1 = Xlrd.open_workbook (' Stu.xls ')
Book2 = Copy (Book1)#A copy of the originalExcel
Sheet = Book2.get_sheet (0) # Get the first few sheet pages
Sheet.write (1,3,0)# Write the rows, columns, and modified values that need to be modified
Sheet.write (1,0,' little Black ')
Book2.save (' Stu.xls ')
Python Action Excel