python3 操作Excel文件

本文介绍了如何使用Python3操作Excel文件,特别是演示了一个实用技巧——将多个Excel工作表合并到一个工作表中,这对于日常的数据处理工作非常有用。
项目中经常用到读写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版本的库

安装方法, 参考库里面的Readme.html 或者Readme.txt

然后可以写功能代码了.
一个简单的功能, 把多个Excel表中的Sheet合并成一个. 这在实际项目中经常用到

#coding=utf-8

import os
import xdrlib, sys
import xlrd
import xlwt3 as xlwt


StringExcelSource = "./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 xlwt3
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/xlwt3/__init__.py", line 3, in <module>
from .workbook import Workbook
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/xlwt3/workbook.py", line 5, in <module>
from .worksheet import Worksheet
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/xlwt3/worksheet.py", line 7, in <module>
from .row import Row
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/xlwt3/row.py", line 8, in <module>
from . import formula
File "/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了.


以上记录, 以备忘!



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值