Traverse a tuple to write Excel, read an Excel file

Source: Internet
Author: User

Pip Install xlrd XLWT

Write Excel

#coding =utf-8

Import XLWT

Workbook = xlwt. Workbook (encoding= ' utf-8 ')

Booksheet = Workbook.add_sheet (' Sheet 1 ', cell_overwrite_ok=true)

Workbook.add_sheet (' Sheet 2 ')

DATA = (' study number ', ' name ', ' age ', ' gender ', ' score '),

(1001, ' AAAA ', at 98, ' male '),

(1002, ' BBBB ', +, ' female ', 90),

(1003, ' CCCC ', ' 100 ', ' female '),

(1004, ' DDDD ', ' a ', ' female ', 86),

(1005, ' eeee ', +, ' female ', 88),)

For I, row in enumerate (DATA):

Forj, col in Enumerate (row):

Booksheet.write (i, J, Col)

Workbook.save (' Chenji.xls ')


Chenji.xls Display

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/76/86/wKioL1ZVdcOiBNgSAAAsTl4mP4g701.png "title=" Chenji.png "alt=" Wkiol1zvdcoibngsaaastl4mp4g701.png "/>



Read Excel

Read Excel


Get a file

Workbook = Xlrd.open_workbook (' transcript. xls ')

Get the number of forms in a file

Workbook.nsheets

Get a form

Workbook.sheets () [i],

Workbook.sheet_by_index (i)

Workbook.sheet_by_name (U ' Sheet1 ')

Get row, number of columns

Sheet.nrows, Sheet.ncols

Get the entire row, column data

Sheet.row (i), Sheet.col (j)

Get a cell data

Sheet.cell (i, j). Value

Sheet.row (i) [J].value

Sheet.col (j) [I].value



#coding =utf-8

Import xlrd


Workbook = Xlrd.open_workbook (' Chenji.xls ')

Print "There is {} sheets in the workbook". Format (workbook.nsheets)

For Booksheet in Workbook.sheets ():

Print Booksheet.name

For row in Xrange (booksheet.nrows):

For Col in Xrange (booksheet.ncols):

Print xlrd.cellname (row, col) #打印列名A1, B2 ...

Print Booksheet.cell (row, col). Value #打印单元格的值


This article is from the DBA Sky blog, so be sure to keep this source http://9425473.blog.51cto.com/9415473/1716804

Traverse a tuple to write Excel, read an Excel file

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.