python3 操作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了.


以上記錄, 以備忘!



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章