Using Python to read and write Excel

Source: Internet
Author: User

A simple example of reading an Excel file

#!/usr/bin/env python#-*-coding:utf-8-*-import xlrdfrom xlrd.book import bookfrom xlrd.sheet import Sheetfrom Xlrd.shee T Import Cellworkbook = Xlrd.open_workbook (' account information. xlsx ') Sheet_names = Workbook.sheet_names () # sheet = Workbook.sheet_by_ Name (' account information ') sheet = workbook.sheet_by_index (1) # Loops All rows of the Excel file for row in Sheet.get_rows ():    # Loops All columns of a row for    col in Row:        # Gets the value in a cell        print (col.value)

Second, a simple example of writing an Excel file

#!/usr/bin/env python#-*-coding:utf-8-*-import xlwtwb = xlwt. Workbook () sheet = wb.add_sheet (' Sheet1 ') for row in range (    5):        sheet.write (Row, col, ' {0} ' line {1} columns '. Format (row, col)) Wb.save (' Xxx.xls ') # more examples: https://github.com/python-excel/xlwt/tree/master/examples

  

  

Using Python to read and write 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.