Python Learning Note (10): Manipulating Excel

Source: Internet
Author: User

First, python operations Excel , Python operations Excel uses XLRD, XLWT, and xlutils modules, XLRD modules are read in Excel, XLWT modules are written in Excel, and xlutils is used to modify Excel. These modules are installed using PIP, the following are the use of these modules.

Second,xlrd module , xlrd module for reading Excel, the specific usage is as follows :

            Importxlrd#Open ExcelWb=xlrd.open_workbook ('abc.xlsx')#Open this Excel must exist, otherwise it will be an error            #get the name of all sheet pages            Print(Wb.sheet_names ())#find a second form by name            #sheet=wb.sheet_by_name (' ABC2 ') #根据sheet页的名字获取sheet页Sheet = wb.sheet_by_index (0)#get sheet page based on index of sheet page            #get the number of rows and columns of a sheet page            Print(sheet.nrows)Print(Sheet.ncols)#Print each line of information             forRowNuminchRange (Sheet.nrows):#Loop through the data for each row                Print(Sheet.row_values (rownum))#fetch data for each row            #print corresponding cell contents by indexCell_a2=sheet.cell (0,1). Value#gets the value of the specified cell, the first value is a column, the second value is a row            Print(CELL_A2)

three, XLWT module , the XLWT module is used to write Excel, write a new Excel

        ImportXLWT title= [            'name','Age','Sex','score'] Stus= [['Mary', 20,'female', 89.9],['Mary', 20,'female', 89.9],['Mary', 20,'female', 89.9],['Mary', 20,'female', 89.9]]        #Create a new Excel objectWBK =XLWT. Workbook ()#Add a sheet page called a curriculumSheet = Wbk.add_sheet ('Stu')         forIinchRange (len (title)):#write to table headerSheet.write (0,i,title[i])#write each row, the first value is the row, the second value is the column, and the third is the value written         forIinchRange (len (stus)):ifI!=0:#if it's not a table header,                 forJinchRange (4): Sheet.write (I,j,stus[i][j])#Loop writes each row of data        #Save the data to the ' Test.xls ' fileWbk.save ('Szz.xls')#save Excel must use the suffix name of. xls, not can be. xlsx

Four, xlutils module ,xlutils module to modify the contents of Excel, can not directly modify the original Excel content, you must first copy a new Excel, and then the new Excel to modify the following usage:

         fromXlrdImportOpen_workbook#To open an Excel module in the Import xlrd module         fromXlutils.copyImportCopy#Import the copy Excel module of the Xlutils moduleRB = Open_workbook ('Szz.xls')        #sheet obtained by Sheet_by_index ()rs =rb.sheet_by_index (0)#Copy an ExcelWB =copy (RB)#by getting to the sheet page inside the new ExcelWS =wb.get_sheet (0) Ws.write (1, 0,'Lily')#write to Excel, the first value is a row, the second value is a columnWb.save ('Szz_new.xls')#To Save the new Excel, save Excel must use the suffix name of. xls, not can be. xlsx

Python Learning Note (10): Manipulating Excel

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.