python 读写 excel

来源:互联网 发布:养生软件下载 编辑:程序博客网 时间:2024/05/23 12:35

      帮朋友写一个excel数据处理脚本,顺便熟悉下python的excel操作


读取excel使用xlrd

下载地址:

http://pypi.python.org/pypi/xlrd

http://www.lexicon.net/sjmachin/xlrd.htm

使用例子:

import xlrd    book = xlrd.open_workbook("myfile.xls")    print "The number of worksheets is", book.nsheets    print "Worksheet name(s):", book.sheet_names()    sh = book.sheet_by_index(0)    print sh.name, sh.nrows, sh.ncols    print "Cell D30 is", sh.cell_value(rowx=29, colx=3)    for rx in range(sh.nrows):        print sh.row(rx)

写入excel使用xlwt,我使用的是xlwt-future-0.8.0,这个支持xlsx

下载地址:

http://pypi.python.org/pypi/xlwt-future

使用例子:

#write work book

outWorkbook = xlwt.Workbook(encoding = 'utf-8')

outsheet = outWorkbook.add_sheet('outcode')

outsheet.write(0, 0, label = 'Row 0, Column 0 Value')

outWorkbook.save('out_Workbook.xls')

需要注意的是:他的一个工作表sheet只能写入65535行,多了就不能写了,解决方法可以是,每65535行新建一个工作表sheet或者向后移动几列,

然后写入,他的列最大值为256,所以最多一个sheet文件可以写入:256 * 65535 个数据


以下附上我的代码例子:

#Auther: lancer

#Data: 2016-01

#Function: ProcessCode

#Version: 1.00


import os

import re

import shutil

import string


#read excel

import xlrd

#write excel

import xlwt


#col defines

COL_LABEL = 0

COL_SCL = 1

COL_SDA = 2

COL_Comment = 3


#mode defines

MODE_COPY = 0

MODE_WRITE = 1


#write work book

outWorkbook = xlwt.Workbook(encoding = 'utf-8')

outsheet = outWorkbook.add_sheet('outcode')

#outsheet.write(0, 0, label = 'Row 0, Column 0 Value')


data = xlrd.open_workbook('Code1023.xlsx')

#

##get worksheet

##table = data.sheets()[0]

##table = data.sheet_by_index(0)

table = data.sheet_by_name(u'Sheet1')

##

##table.row_values(i)

##table.col_values(i)

##

##

##

##    nrows = table.nrows

##        

##        ncols = table.ncols

##        

##

##        for i in range(nrows ):

##            print table.row_values(i)

##

#

##get cell

#cell_A1 = table.cell(0,0).value

#

#print cell_A1

#

#cell_2d = table.cell(2,3).value

#

#print cell_2d

#

#ncols = table.nrows

#

#print ncols

#

#inputSheet = data.sheet_by_name(u'Sheet_in')

#outSheet = data.sheet_by_name(u'Sheet_out')


def processOneCode(code, start, end, rows):

    st3end = 274


    outStart = (st3end - start) * code + start

    mode = MODE_COPY

    byteIndex = 0

    xIndex = 0

    print code

#    print outStart


    colStart = code/rows*5

    if code/rows >0:

        outStart = (code%rows) * (st3end - start)

#        print "---outstart", outStart

    #label only need to write once

    outsheet.write(outStart, COL_LABEL+colStart, label ='Code' + str(code) + '_ST2')


    outrow = outStart

#write step 2

    for iin range(start, end):

#        print "i", i

#        print "outstart", outStart

        outrow = outStart + i - start

#        print "outrow", outrow

#        outsheet.write(outrow, COL_LABEL, label = table.cell(i, COL_LABEL).value)

        outsheet.write(outrow, COL_SCL+colStart, label = table.cell(i, COL_SCL).value)

        #write comment

        commentValue = table.cell(i, COL_Comment).value

        outsheet.write(outrow, COL_Comment+colStart, label = commentValue)

        commentValue = commentValue.strip().upper()

#        print commentValue

        if'X'==commentValue:

            xIndex += 1

#            print "find x:" + commentValue + str(xIndex)


        if mode == MODE_COPY:

            outsheet.write(outrow, COL_SDA+colStart, label = table.cell(i, COL_SDA).value)

            if xIndex==2:

                mode = MODE_WRITE

                xIndex = 0

        else:

            if'X'==commentValue or'ACK'==commentValue:

                outsheet.write(outrow, COL_SDA+colStart, label = table.cell(i, COL_SDA).value)

            elif'STOP'==commentValue:

                outsheet.write(outrow, COL_SDA+colStart, label = table.cell(i, COL_SDA).value)

                mode = MODE_COPY

            else:

                byteIndex += 1

#                if code != 0:

#                    print str(code) + 'code:', (code >> (10-byteIndex)) & 1

                outsheet.write(outrow, COL_SDA+colStart, label = (code >> (10-byteIndex)) &1)

                if byteIndex ==10:

                    byteIndex = 0


#write step3

    outStart = outStart + end - start

#    print "step3 start:",outStart

    outsheet.write(outStart, COL_LABEL+colStart, label ='Code' + str(code) + '_ST3')

    for iin range(end, st3end):

        outrow = outStart + i - end

        outsheet.write(outrow, COL_SCL+colStart, label = table.cell(i, COL_SCL).value)

        outsheet.write(outrow, COL_SDA+colStart, label = table.cell(i, COL_SDA).value)

        outsheet.write(outrow, COL_Comment+colStart, label = table.cell(i, COL_Comment).value)


#write before process

for i in range (32):

    for jin range (4):

        outsheet.write(i, j, label = table.cell(i, j).value)


#write insert process

for k in range (0,1024):

    processOneCode(k, 32,243, 200)


#for i in range (32, 243):

##write col0

#    outsheet.write(i, 0, label = table.cell(i, 0).value)

##write col1

#    outsheet.write(i, 1, label = table.cell(i, 1).value)




#outSheet.cell(i+start, COL_Comment) = tabel.cell(i+start, COL_Comment)


outWorkbook.save('out_Workbook.xls')




0 0