Excel Python read-write

Source: Internet
Author: User

You might need to sort out some documents in your actual work, or record some data, and using Python to manipulate Excel might help you.

Read operation:

#Encoding:utf-8 #设置编码方式ImportXlrd#Import xlrd Module#Open the Excel file for the specified file pathXlsfile= R'D:\AutoPlan\apisnew.xls' Book= Xlrd.open_workbook (Xlsfile)#get the Book object for Excel#There are 2 ways to get sheet objects:Sheet_name=book.sheet_names () [0]#gets the sheet name of the specified indexPrintSheet_namesheet1=book.sheet_by_name (Sheet_name)#get through sheet name, of course, if you know sheet name, you can specify it directly.Sheet0=book.sheet_by_index (0)#get sheet objects by sheet index#get the number of rows and columns:nrows= Sheet.nrows#Total RowsNcols = Sheet.ncols#total number of columns#gets the value of the specified row, column, and return object as a list of valuesRow_data= sheet.row_values (0)#get the data list for line 1thCol_data = sheet.col_values (0)#get the list of data in the first column, and then you can iterate over the data.#gets the value of the specified cell through the cell's position coordinatesCell_value1 = Sheet.cell_value (0,1)##只有cell的值内容, such as: Http://xxx.xxx.xxx.xxx:8850/2/photos/square/Printcell_value1cell_value2= Sheet.cell (0,1)##除了cell值内容外还有附加属性, such as: Text:u ' http://xxx.xxx.xxx.xxx:8850/2/photos/square/'PrintCell_value2

Write operation:

#Encoding:utf-8 #设置编码方式  ImportXLWTWBK= XLWT. Workbook (encoding='Utf-8', style_compression=0) Sheet= Wbk.add_sheet ('Sheet 1', cell_overwrite_ok=true)##第二参数用于确认同一个cell单元是否可以重设值. Sheet.write (0,0,'some text') Sheet.write (0,0,'This should overwrite')##重新设置, Need cell_overwrite_ok=truestyle=XLWT. Xfstyle () Font=XLWT. Font () Font.Name='Times New Roman'Font.Bold=Truestyle.font=fontsheet.write (0,1,'some bold times text', Style) Wbk.save ('D:\TestData2.xls')##该文件名必须存在

Just start learning, we share ~ code to from http://blog.csdn.net/five3/article/details/7034826

Official information:

: http://pypi.python.org/pypi/xlrd

Website address: http://www.python-excel.org/

Document Address: https://secure.simplistix.co.uk/svn/xlrd/trunk/xlrd/doc/xlrd.html

Document PDF Download: http://www.simplistix.co.uk/presentations/python-excel.pdf

Excel Python read-write

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.