在python中使用xlrd,xlwt读写excel
2018-01-22 00:00
645 查看
1.xlrd读取excel
# -*- coding: utf-8 -*- import xdrlib ,sys import xlrd def open_excel(file= 'file.xls'): try: data = xlrd.open_workbook(file) return data except Exception as e: print (str(e)) #根据索引获取Excel表格中的数据 参数:file:Excel文件路径 colnameindex:表头列名所在行的所以 ,by_index:表的索引 def excel_table_byindex(file= 'file.xls',colnameindex=0,by_index=0): data = open_excel(file) table = data.sheets()[by_index] nrows = table.nrows #行数 ncols = table.ncols #列数 colnames = table.row_values(colnameindex) #某一行数据 list =[] for rownum in range(1,nrows): row = table.row_values(rownum) if row: app = {} for i in range(len(colnames)): app[colnames[i]] = row[i] list.append(app) return list #根据名称获取Excel表格中的数据 参数:file:Excel文件路径 colnameindex:表头列名所在行的所以 ,by_name:Sheet1名称 # def excel_table_byname(file= 'file.xls',colnameindex=0,by_name=u'Sheet1'): # data = open_excel(file) # table = data.sheet_by_name(by_name) # nrows = table.nrows #行数 # colnames = table.row_values(colnameindex) #某一行数据 # list =[] # for rownum in range(1,nrows): # row = table.row_values(rownum) # if row: # app = {} # for i in range(len(colnames)): # app[colnames[i]] = row[i] # list.append(app) # return list def main(): tables = excel_table_byindex() for row in tables: print (row) # tables = excel_table_byname() # for row in tables: # print (row) if __name__=="__main__": main()
![](https://static.oschina.net/uploads/space/2018/0122/144421_d1mI_3018050.png)
2.xlwt写入excel
# -*- coding: utf-8 -*- # 导入xlwt模块 import xlwt # 创建一个Workbook对象,这就相当于创建了一个Excel文件 book = xlwt.Workbook(encoding='utf-8', style_compression=0) ''' Workbook类初始化时有encoding和style_compression参数 encoding:设置字符编码,一般要这样设置:w = Workbook(encoding='utf-8'),就可以在excel中输出中文了。 默认是ascii。当然要记得在文件头部添加: #!/usr/bin/env python # -*- coding: utf-8 -*- style_compression:表示是否压缩,不常用。 ''' # 创建一个sheet对象,一个sheet对象对应Excel文件中的一张表格。 # 在电脑桌面右键新建一个Excel文件,其中就包含sheet1,sheet2,sheet3三张表 sheet = book.add_sheet('test', cell_overwrite_ok=True) # 其中的test是这张表的名字,cell_overwrite_ok,表示是否可以覆盖单元格,其实是Worksheet实例化的一个参数,默认值是False # 向表test中添加数据 sheet.write(0, 0, 'EnglishName') # 其中的'0-行, 0-列'指定表中的单元,'EnglishName'是向该单元写入的内容 sheet.write(1, 0, 'Marcovaldo') txt1 = '中文名字' sheet.write(0, 1, txt1.encode().decode('utf-8')) # 此处需要将中文字符串解码成unicode码,否则会报错 txt2 = '马可瓦多' sheet.write(1, 1, txt2.encode().decode('utf-8')) # 最后,将以上操作保存到指定的Excel文件中 book.save(r'f:\test1.xls') # 在字符串前加r,声明为raw字符串,这样就不会处理其中的转义了。否则,可能会报错
![](https://static.oschina.net/uploads/space/2018/0122/144441_7fYF_3018050.png)
相关文章推荐
- python操作Excel读写--使用xlrd和xlwt
- python 6-5 如何读写excel文件 使用第三方库xlrd 和 xlwt,这两个库分别用于excel读和写
- python读写Excel文件--使用xlrd模块读取,xlwt模块写入
- 使用Python xlrd与xlwt模块读写Excel
- [Python笔记] 使用xlrd和xlwt进行简单的excel表格读写操作
- python中使用xlrd、xlwt读写excel(xls格式)
- 使用python读写操作同一个excel(xlrd,xlwt,xlutils)
- python使用xlrd与xlwt对excel的读写和格式设定
- python excel 的相关操作(使用 xlrd xlwt 读写)
- 使用python读写操作同一个excel(xlrd,xlwt,xlutils)
- Python 使用 Xlrd/xlwt 操作 Excel 分类: python基础学习 2014-03-17 12:06 958人阅读 评论(0) 收藏
- python操作Excel读写--使用xlrd
- python操作Excel读写--使用xlrd
- Python 使用 Xlrd/xlwt 操作 Excel
- python操作Excel读写--使用xlrd
- python操作Excel读写(使用xlrd和xlrt)
- python操作Excel读写--使用xlrd
- python操作Excel读写--使用xlrd(转)
- python操作Excel读写--使用xlrd
- python操作Excel读写--使用xlrd