python3 操作Excel文件
来源:互联网 发布:金庸小说 知乎 编辑:程序博客网 时间:2024/04/28 14:08
项目中经常用到读写Excel的功能,之前用Java写过一个. 但感觉还是太繁琐, 尤其是需要增加点功能的时间, 还得开一个工程(比如Eclipse)写代码, 编译, 导出jar文件. 然后才能使用. 最近发现用python读取起来更方便快捷一些.
直接改代码, 改完就能测.
我是用python3来做的这个工程, 当然python2也没问题, 而且因为第三方库的问题, 对python2的支持可能会更好一些. 个人习惯, 这里选择了python3
首先安装python3 下载地址
再下载两个需要的第三方库
xlrd https://pypi.python.org/pypi/xlrd 两个python版本都支持
xlwt https://pypi.python.org/pypi/xlwt3 仅是python3版本的库
编译, 问题出现了.
它的解决方法有点怪,找到这个文件, (因为python版本问题, 和MacOs版本问题, 可能有出入)
/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/xlwt3/formula.py
这样修改一下就ok了.
以上记录, 以备忘!
直接改代码, 改完就能测.
我是用python3来做的这个工程, 当然python2也没问题, 而且因为第三方库的问题, 对python2的支持可能会更好一些. 个人习惯, 这里选择了python3
首先安装python3 下载地址
再下载两个需要的第三方库
xlrd https://pypi.python.org/pypi/xlrd 两个python版本都支持
xlwt https://pypi.python.org/pypi/xlwt3 仅是python3版本的库
安装方法, 参考库里面的Readme.html 或者Readme.txt
然后可以写功能代码了.
一个简单的功能, 把多个Excel表中的Sheet合并成一个. 这在实际项目中经常用到
#coding=utf-8import osimport xdrlib, sysimport xlrdimport xlwt3 as xlwtStringExcelSource = "./tables/string_语言包.xls"StringExcelTarget = "./tables/string_final.xls"RowIndex_Data = 3#单元格内容转为字符串def getText(value): if(type(value) == float): return str(int(value)) return str(value)def open_excel(p_file): try: data = xlrd.open_workbook(p_file) return data except Exception as ex: print (str(ex))def makeSheetHead(p_excelSheetTarget): l_excelSheetTarget.write(0, 0, "键") l_excelSheetTarget.write(0, 1, "说明") l_excelSheetTarget.write(0, 2, "英文") l_excelSheetTarget.write(0, 3, "中文") l_excelSheetTarget.write(1, 0, "id") l_excelSheetTarget.write(1, 1, "des") l_excelSheetTarget.write(1, 2, "en") l_excelSheetTarget.write(1, 3, "cn") l_excelSheetTarget.write(2, 0, "client") l_excelSheetTarget.write(2, 1, "N") l_excelSheetTarget.write(2, 2, "client") l_excelSheetTarget.write(2, 3, "client")if __name__ == "__main__": print ("make string_final excel file ") l_excelDataTarget = xlwt.Workbook() l_excelSheetTarget = l_excelDataTarget.add_sheet("dict_string") makeSheetHead(l_excelSheetTarget) l_rowIndexTarget = RowIndex_Data l_excelData = open_excel(StringExcelSource) for l_sheet in l_excelData.sheets(): if(l_sheet.name.startswith("string_")): #每一行数据进行遍历 for l_rowIndex in range(RowIndex_Data, l_sheet.nrows): l_cellId = l_sheet.cell(l_rowIndex, 0).value l_cellId = getText(l_cellId) if (len(l_cellId) <= 0): continue for l_colIndex in range(0, l_sheet.ncols): l_excelSheetTarget.write(l_rowIndexTarget, l_colIndex, l_sheet.cell(l_rowIndex, l_colIndex).value) l_rowIndexTarget += 1 l_excelDataTarget.save(StringExcelTarget)
编译, 问题出现了.
Traceback (most recent call last):File "/Users/tcp/Documents/Python/Working/Menu.py", line 6, in <module>import xlwt3File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/xlwt3/__init__.py", line 3, in <module>from .workbook import WorkbookFile "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/xlwt3/workbook.py", line 5, in <module>from .worksheet import WorksheetFile "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/xlwt3/worksheet.py", line 7, in <module>from .row import RowFile "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/xlwt3/row.py", line 8, in <module>from . import formulaFile "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/xlwt3/formula.py", line 6, in <module>class Formula(object):ValueError: '__init__' in __slots__ conflicts with class variable
它的解决方法有点怪,找到这个文件, (因为python版本问题, 和MacOs版本问题, 可能有出入)
/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/xlwt3/formula.py
#__slots__ = ["__init__", "__s", "__parser", "__sheet_refs", "__xcall_refs"] #>=-Rct-=< __slots__ = ["__s", "__parser", "__sheet_refs", "__xcall_refs"]
这样修改一下就ok了.
以上记录, 以备忘!
0 0
- python3 操作Excel文件
- python3 操作excel
- python3 操作excel 写入xlsx类型文件 openpyxl
- Python3导入Excel文件
- Python3之读写Excel操作
- python3的文件操作
- python3 文件操作
- python3操作csv文件
- file 文件操作 python3
- python3 操作文件相关
- Python3之文件操作
- Python3文件io操作
- python3 文件操作
- Python3文件操作
- python3 文件操作
- Python3 Xlrd 读取EXCEL 操作笔记
- python3,用win32com操作excel及其宏
- python3使用 openpyxl 操作excel表格
- 线段树 csu1551 Longest Increasing Subsequence Again
- Mapped Statements collection does not contain value for(mybatis框架项目)
- 恢复误删除的数据
- phalapi-入门篇5(数据库操作和Model层)
- [Maven]Maven介绍,包括作用、核心概念、用法、常用命令、扩展及配置
- python3 操作Excel文件
- POJ 题目3254 Corn Fields(状压DP)
- ArrayList,LinkedList,Vestor区别
- Delphi XE7 学习笔记 StopWatch类
- 小样本分析(三)
- 使用JAVA操作HIVE
- 登陆
- UICollectionView 以及 AWCollectionViewDialLayout
- String,StringBuffer, StringBuilder 的区别