Python's Excel read-write operation

Source: Internet
Author: User

One, xlrd and XLWT installation

1. Download XLWT installation package https://pypi.org/project/xlwt/#files

2. Enter the file directory after decompression

3. Execute Python setup.py install

Second, read operation

1 #-*-conding:utf-8-*-2 __author__=' DSH'3 #How to read from an Excel using XLRD module4 Importxlrd5 #associates the XLS file in the specified path to get the book object6file_name ="Name.xls"7 #opens the specified file, creating a File object8Book =Xlrd.open_workbook (file_name)9 #get sheet objects by sheet indexTenSheet1 =book.sheet_by_index (0) One ## Gets the sheet name of the specified index A #sheet1_name = book.sheet_names () [0] - #print (sheet1_name) - ## Get Sheet object by sheet name the #Sheet1 = book.sheet_by_name (sheet1_name) - #get the number of rows and columns - #total number of rows -nrows =sheet1.nrows + #total number of columns -Ncols =Sheet1.ncols + #traverse the contents of a printed table A  forIinchRange (nrows): at    forJinchRange (ncols): -Cell_value =Sheet1.cell_value (i, J) -     Print(Cell_value, end ="\ t") -   Print("")

Third, write operation

1 #-*-conding:utf-8-*-2 __author__=' DSH'3 #How to write to an Excel using XLWT module4 ImportXLWT5 #Create an Wordbook object that is equivalent to creating an Excel file6Book = XLWT. Workbook (encoding ="Utf-8", style_compression =0)7 #Create a Sheet object, a sheet object that corresponds to a table in an Excel file8Sheet = Book.add_sheet ("Sheet1", CELL_OVERWRITE_OK =True)9 #adding data to the table Sheet1TenSheet.write (0, 0,"Englishname")#where, "0, 0" specifies the cell in the table, "Englishname" is the content written to the cell OneSheet.write (1, 0,"MaYi") ASheet.write (0, 1,"Chinese name") -Sheet.write (1, 1,"ants") - #Finally, save the above actions to the specified Excel file theBook.save ("Name.xls")

Iv. real-Name document format conversion Read and write case

#!/usr/bin/python#-*-coding:utf-8-*-ImportxlrdImportXLWTImportSYSImportOS__author__='DSH'file_name="1111.xls"Book_read=Xlrd.open_workbook (file_name) Sheet_read=book_read.sheet_by_index (0) Nrows_read=Sheet_read.nrowsncols_read=Sheet_read.ncolsbook_write= XLWT. Workbook (encoding ="Utf-8", style_compression =0) Sheet_write= Book_write.add_sheet ("Sheet1", CELL_OVERWRITE_OK =True)#write the No. 0 column to column 1th . forIinchRange (nrows_read): Cell_value=Sheet_read.cell_value (i,0) sheet_write.write (i,1, Cell_value)#write the 10th column of data to column 3rd forIinchRange (nrows_read): Cell_value= Sheet_read.cell_value (i,10) Sheet_write.write (i,3, Cell_value) Book_write.save ("2222.xls")

Python's Excel read-write operation

Related Article

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.