Day7-python Learning Notes (16) Excel operations

Source: Internet
Author: User

Import XLWT #写入excel
# book = XLWT. Workbook () #新建一个excel
# sheet = book.add_sheet (' Sheet1 ') #添加一个sheet页
# Sheet.write (0,0, ' name ')
# sheet.write (0,1, ' sex ')
# Sheet.write (0,2, ' age ')
# book.save (' Stu.xls ') #微软的office不能用xlsx结尾的, WPS casual


Stus = [
[' name ', ' age ', ' gender ', ' score '],
[' Mary ', 20, ' female ', 89.9],
[' Mary ', 20, ' female ', 89.9],
[' Mary ', 20, ' female ', 89.9],
[' Mary ', 20, ' female ', 89.9]
]
Book = XLWT. Workbook () #新建一个excel
sheet = book.add_sheet (' Sheet1 ') #添加一个sheet页
raw = 0# control line
For Stu in Stus:
cols = 0 #控制列
For s in Stu:
sheet.write (raw,cols,s)
cols+=1
raw+=1

book.save (' Kkk.xls ')

  import xlrd# read Excel   
book = Xlrd.open_workbook (' Stu.xls ') #打开一个excel
sheet = Book.sheet_by_index (0) #根据顺序获取sheet
# Sheet2 = book.sheet_by_name (' Sheet1 ') #根据sheet页名字获取sheet
# print (Sheet.cell (0,0). Value) #指定行和列获取数据
# print (sheet.ncols) #获取excel里面有多少列
# print (sheet.nrows) #获取excel里面有多少行
Sheet.row_values (1) #取第几行的数据
print (sheet.col_values (1)) #取第几列的数据
for I in Range (sheet.nrows): # 0 1 2 3 4 5
PR Int (sheet.row_values (i)) #取第几行的数据


From xlutils.copy import copy  #修改excel
Import xlrd
Book1 = Xlrd.open_workbook (' Stu.xls ')
Book2 = Copy (Book1) #拷贝一份原来的excel
Sheet = book2.get_sheet (0) #获取第几个sheet页
Sheet.write (1,3,0)
Sheet.write (1, 0, ' Little black ')
Book2.save (' Stu.xls ')









Day7-python Learning Notes (16) Excel operations

Related Article

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.