Python writes data to Excel

Source: Internet
Author: User

Summary Link:

Python third-party libraries--xlrd and XLWT action Excel file learning: http://blog.csdn.net/wangkai_123456/article/details/50457284

Python operations Excel Read-write (using XLRD and XLRT): http://blog.csdn.net/mr__fang/article/details/7089581

In Python, you typically use XLRD (Excel read) to read an Excel file, use XLWT (Excel write) to generate an Excel file (you can control the formatting of cells in Excel), and be aware that Reading Excel with XLRD cannot operate on it: the Xlrd.open_workbook () method returns XLRD. Book type, which is read-only and cannot be manipulated. and XLWT. Workbook () returns the XLWT. The workbook type of Save (filepath) method can save an Excel file.

So it's easy to handle reading and generating Excel files, but making changes to an existing Excel file can be tricky. However, there is also a xlutils (dependent on xlrd and XLWT) that provides the ability to copy the contents of an Excel file and modify the file. Its reality is only in xlrd. There was a pipeline between book and Xlwt.workbook.

The copy () method of the Xlutils.copy module implements this function, and the sample code is as follows:

 fromXlrdImportOpen_workbook fromXlutils.copyImportCopy RB= Open_workbook ('M:\\1.xls') #sheet obtained by Sheet_by_index () does not have the write () methodrs =rb.sheet_by_index (0) WB=copy (RB)#sheet with the Write () method obtained through Get_sheet ()WS =wb.get_sheet (0) ws.write (0, 0,'changed!') Wb.save ('M:\\1.xls')

Practice Code (Read & write via xlrd, and save with copy):

Special Note: because copy save is essentially saved by XLWT, the file is actually XLWT saved.

The XLWT can only be written to an XLS file and cannot be written to an xlsx file.

Importxlrd fromXlwtImport* fromXlutils.copyImportCopyxlsfile='Test.xls' Book=Xlrd.open_workbook (xlsfile) sheet_name=book.sheet_names ()Print(sheet_name) sheet= Book.sheet_by_index (1) nrows=Sheet.nrowsncols=Sheet.ncolsPrint(nrows)Print(ncols) Row_data=sheet.row_values (0) Col_data=sheet.col_values (0)Print(Row_data)Print(col_data) Cell_value= Sheet.cell_value (3, 0)Print(cell_value) cell_value2= Sheet.cell (3, 0)Print(cell_value2) Sheet.put_cell (1,2,1,"Test", 0) cell_value2= Sheet.cell ()Print(cell_value2)#Save XlsfileWB =copy (book) Wb.save (Xlsfile)

Python writes data to 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.