python 读写 excel

      帮朋友写一个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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值