Python operations Excel

Source: Internet
Author: User

Read Excel

1. Import Module

Import xlrd

2. Open Excel file to read data

data = Xlrd.open_workbook (' Excel.xls ')

3. Get a Worksheet

①table = data.sheets () [0] #通过索引顺序获取
②table = Data.sheet_by_index (0) #通过索引顺序获取
③table = data.sheet_by_name (U ' Sheet1 ') #通过名称获取


4. Get values for entire rows and columns (return array)

Table.row_values (i)
Table.col_values (i)


5. Get the number of rows and columns

table.nrowsTable.ncols 6, Get cell
Table.cell (0,0). ValueTable.cell (2,3). Value Write Excel

1. Import Module

Import XLWT
2, create workbook (actually Excel, then save a bit on the line)
workbook = xlwt. Workbook (encoding = ' ASCII ')
3. Create a table
worksheet = workbook.add_sheet (' My worksheet ')
4. Write content to cell
worksheet.write (0, 0, label = ' Row 0, Column 0 Value ')
5. Save
workbook.save (' Excel_workbook.xls ') write data to an existing Excel
From XLRD import open_workbookfrom xlutils.copy import Copy RB = Open_workbook (' M:\\1.xls ') #通过sheet_by_index () Gets the sheet without the Write () Method rs = rb.sheet_by_index (0) WB = Copy (RB) #通过get_sheet () Gets the sheet with the Write () method ws = Wb.get_sheet (0) Ws.write (0, 0, ' changed! ') wb.save (' M:\\1.xls ')

Python operations 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.