Is the way Python reads, writes, and writes Excel files

Source: Internet
Author: User

Three tool kits

The Python three Toolkit for Excel works as follows

    • xlrd: read-related operations on Excel
    • xlwt: Write-related operations on Excel
    • xlutils: Integration of Excel read and write operations

Note that it can only be manipulated .xls and cannot be manipulated .xlsx .

These three toolkits are available for download directly using PIP:

sudo pip install xlrdsudo pip install xlwtsudo pip install Xlutils

XLS file Write-in

XLWT can only create a completely new Excel file, and then write to and save the file.

But most of the time we want to read in an Excel file and then modify or append it, and then it needs to be xlutils.

Simple use of xlutils

The following demo is to append content to an Excel file:

 fromXlrdImportOpen_workbook fromXlutils.copyImportCopyrexcel= Open_workbook ("Collection.xls")#read an Excel file using the method provided by Wlrdrows = Rexcel.sheets () [0].nrows#use the method provided by WLRD to get the number of rows you have nowExcel = Copy (Rexcel)#convert XLRD objects to XLWT objects using the Copy method provided by XlutilsTable = Excel.get_sheet (0)#get the sheet you want to manipulate by using the Xlwt object methodvalues = ["1","2","3"]row=rows forValueinchvalues:table.write (row, 0, value)#XLWT The Write method of the object, the parameters are row, column, valueTable.write (row, 1,"haha") table.write (row,2,"Lala") Row+ = 1Excel.save ("Collection.xls")#The XLWT object is saved by overwriting the original Excel

Is the way Python reads, writes, and writes Excel files

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.