开始使用xlrd,下载安装包,解压,安装省略 下载http://pypi.python.org/pypi/xlrd python
假设个人表格文件叫demo.xls,三个sheet,第一个sheet内容以下git
则要访问3行第D列单元格则使用以下代码 ---------------------------------------------------------------------------- >>> import xlrd >>> import os >>> import sys >>> wb=xlrd.open_workbook("demo.xls") >>> range(wb.nsheets) #能够看到,一共有三个sheet,其索引为0,1,2 [0, 1, 2] >>> sht1=wb.sheet_by_name("demo_sheet1") #等同于使用sht2=wb.sheet_by_index(0) >>> print sht1.cell_value(2,3) #索引从0开始,先行,后列,D4对应与(2,3) 是 >>> print sht1.cell_value(4,1) #B5对应(4,1) 餐饮 >>> for item in sht1.row_values(1): #能够输出整个一行,返回为列表 print item, 4.0 电视剧 web 是
=========xlwt 写============web
xlwt是python的一个能够写生成excel表格文件的module。主要特色在于写的功能比较强大,提供了表单行数、列数、单元格数据类型等,在使用python的时候import xlrd就能够,下载 http://pypi.python.org/pypi/xlwt,编写以下代码:函数
import xlwt测试
from datetime import datetime大数据
style0=xlwt.easyxf('font: name Times New Roman, color-index red, bold on', num_format_str='#,##0.00')spa
style1=xlwt.easyxf(num_format_str='D-MMM-YY')excel
wb = xlwt.Workbook()code
ws=wb.add_sheet("A Test Sheet")orm
ws.write(0,0,1234.56,style0)
ws.write(1,0,datetime.now(), style1)
ws.write(2,0,datetime.now()) #A3
ws.write(2,1,datetime.now()) #B3
ws.write(2,2,xlwt.Formula("A3+B3"))
wb.save("example.xls")
生成一个名为”example.xls“的表格,以下: 其中C3(2行2列) 使用了公式 “=B2+B3”,很帅吧
写了两个和坐标有关的函数def strPosition2Coordinate(strCoordinate = "A1"): ''' A,B....Z,AA,AB,AC....AZ,BA,内部将A-Z 与0-25对应,那么这个列至关于26进制的整数 列字母限制在2位,且只支持到256列,即IV,如AB1,A3合法,ABC1,IW, IX等非法。 ''' nSplitPos = len(re.split("[0-9]+", strCoordinate)[0]) strRow, strCol = strCoordinate[nSplitPos:], strCoordinate[:nSplitPos] assert(strCol.isupper() and strRow.isdigit() and len(strCol) <= 2) idx = 0 for item in "ABCDEFGHIJKLMNOPQRSTUVWXYZ": exec("%s=%d" % (item, idx)); idx += 1 nRow, nCol = (int(strRow) - 1, 0) if len(strCol) == 1: exec("nCol = %s" % strCol) if len(strCol) == 2: exec("nCol = (%s + 1) * 26 + (%s + 1) - 1 " % (strCol[0], strCol[1])) assert(nCol <= 255) # return (nRow, nCol) def strCoordinate2Position(nRow = 0, nCol = 0): strCoordinate = "" assert(nCol <= 675) strBasicLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" nQuotient, nRemainder = nCol / 26, nCol % 26 if nQuotient == 0: strCoordinate = strBasicLetters[nRemainder] + str(nRow + 1) if nQuotient != 0: strCoordinate = strBasicLetters[nQuotient - 1] + strBasicLetters[nRemainder] + str(nRow + 1) return strCoordinate 从example里提出的一些常见的功能函数,使用的时候能够参考:echo python big-16Mb.py #大数据文件 echo python big-35Mb.py #大数据文件 python blanks.py #虚框,中划线等 python col_width.py #设置列距 python country.py # python dates.py #各类日期及格式 python format.py #颜色,中划线,加粗 python formulas.py #公式 python formula_names.py # python hyperlinks.py #超连接 python image.py #插入图片 python merged.py #合并单元格和双线边框 python merged0.py #合并单元格 python merged1.py #边框线 python mini.py #最小测试 python num_formats.py #数字格式,颜色 python outline.py python panes.py #黑线和分隔 python parse-fmla.py python protection.py #不知道什么鸟东东 python row_styles.py #设置不一样的行距 fnt = Font();fnt.height = i*20;style = XFStyle();style.font = fnt;ws.row(i).set_style(style) python row_styles_empty.py python simple.py python sst.py python unicode0.py #Оля python unicode1.py #不知道什么鸟字符 python unicode2.py #汉语汉字,特殊字符等 python wsprops.py python xlwt_easyxf_simple_demo.py