标签:数据 获得 文档 setup.py from ESS strong range 索引
一、xlrd和xlwt安装
1、下载xlwt安装包https://pypi.org/project/xlwt/#files
2、解压后进入文件目录
3、执行python setup.py install
二、读取操作
1 # -*- conding:utf-8 -*- 2 __author__ = ‘dsh‘ 3 # How to read from an Excel using xlrd module 4 import xlrd 5 # 关联指定路径中的xls文件,得到book对象 6 file_name = "name.xls" 7 #打开指定文件,创建文件对象 8 book = xlrd.open_workbook(file_name) 9 # 通过sheet索引获得sheet对象 10 sheet1 = book.sheet_by_index(0) 11 # # 获得指定索引的sheet名 12 # sheet1_name = book.sheet_names()[0] 13 # print(sheet1_name) 14 # # 通过sheet名字获得sheet对象 15 # sheet1 = book.sheet_by_name(sheet1_name) 16 # 获得行数和列数 17 # 总行数 18 nrows = sheet1.nrows 19 #总列数 20 ncols = sheet1.ncols 21 # 遍历打印表中的内容 22 for i in range(nrows): 23 for j in range(ncols): 24 cell_value = sheet1.cell_value(i, j) 25 print(cell_value, end = "\t") 26 print("")
三、写入操作
1 # -*- conding:utf-8 -*- 2 __author__ = ‘dsh‘ 3 #How to write to an Excel using xlwt module 4 import xlwt 5 #创建一个Wordbook对象,相当于创建了一个Excel文件 6 book = xlwt.Workbook(encoding = "utf-8", style_compression = 0) 7 #创建一个sheet对象,一个sheet对象对应Excel文件中的一张表格 8 sheet = book.add_sheet("sheet1", cell_overwrite_ok = True) 9 #向表sheet1中添加数据 10 sheet.write(0, 0, "EnglishName") #其中,"0, 0"指定表中的单元格,"EnglishName"是向该单元格中写入的内容 11 sheet.write(1, 0, "MaYi") 12 sheet.write(0, 1, "中文名字") 13 sheet.write(1, 1, "蚂蚁") 14 #最后,将以上操作保存到指定的Excel文件中 15 book.save("name.xls")
四、实名制文档格式转换读写案例
#!/usr/bin/python # -*- coding: utf-8 -*- import xlrd import xlwt import sys import os __author__ = ‘dsh‘ file_name = "1111.xls" book_read = xlrd.open_workbook(file_name) sheet_read = book_read.sheet_by_index(0) nrows_read = sheet_read.nrows ncols_read = sheet_read.ncols book_write = xlwt.Workbook(encoding = "utf-8",style_compression = 0) sheet_write = book_write.add_sheet("sheet1",cell_overwrite_ok = True) #把第0列写到第1列 for i in range(nrows_read): cell_value = sheet_read.cell_value(i,0) sheet_write.write(i,1,cell_value) #把第10列数据写到第3列 for i in range(nrows_read): cell_value = sheet_read.cell_value(i,10) sheet_write.write(i,3,cell_value) book_write.save("2222.xls")
标签:数据 获得 文档 setup.py from ESS strong range 索引
原文地址:https://www.cnblogs.com/he-ding/p/9355510.html