Python operations Excel

Source: Internet
Author: User

#读excel
Import xlrd
Book = Xlrd.open_workbook (R ' students.xlsx ')
#打开excel
Print (Book.sheet_names ())
#获取所有sheet的名字
Sheet = book.sheet_by_index (0)
#根据sheet页的位置去取sheet
Sheet2 = Book.sheet_by_name (' Sheet2 ')
#根据sheet页的名字获取sheet页
Print (sheet.nrows) #获取sheet页里面的所有行数
Print (Sheet.ncols) #获取sheet页里面的所有列数
Print (sheet.row_values (0))
#根据行号获取整行的数据
Print (sheet.col_values (0))
#根据列获取整列的数据
Print (Sheet.cell (). Value)
#cell方法是获取指定单元格的数据, preceded by rows, followed by columns
#读excel的时候, XLS xlsx can be read


#写excel

Import XLWT
Book = XLWT. Workbook ()
#新建一个excel对象
Sheet = book.add_sheet (' stu ')
#添加一个sheet页
Sheet.write (0,0, ' number ') #行的下标, column subscript, fill in the contents
Book.save (' Stu.xls ')
#写excel的时候, only XLS can be manipulated

#修改Excel, a new copy of the Excel object is required
From xlutils.copy Import copy
Book = Xlrd.open_workbook (' New_stu.xls ')
#打开原来的excel
New_book = Copy (book)
#通过xlutils里面copy复制一个excel对象
Sheet = new_book.get_sheet (0)
#获取sheet页
Sheet.write (0,0, ' id ')
New_book.save (' New_stu_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.