用python读写excel的方法
本文实例讲述了用python读写excel的方法。分享给大家供大家参考。具体如下:
最近需要从多个excel表里面用各种方式整理一些数据,虽然说原来用过java做这类事情,但是由于最近在学python,所以当然就决定用python尝试一下了。发现python果然简洁很多。这里简单记录一下。(由于是用到什么学什么,所以不算太深入,高手勿喷,欢迎指导)
一、读excel表
读excel要用到xlrd模块,官网安装(http://pypi.python.org/pypi/xlrd)。然后就可以跟着里面的例子稍微试一下就知道怎么用了。大概的流程是这样的:
1、导入模块
importxlrd
2、打开Excel文件读取数据
data=xlrd.open_workbook('excel.xls')
3、获取一个工作表
① table=data.sheets()[0] #通过索引顺序获取
② table=data.sheet_by_index(0)#通过索引顺序获取
③ table=data.sheet_by_name(u'Sheet1')#通过名称获取
4、获取整行和整列的值(返回数组)
table.row_values(i) table.col_values(i)
5、获取行数和列数
table.nrows table.ncols
6、获取单元格
table.cell(0,0).value table.cell(2,3).value
就我自己使用的时候觉得还是获取cell最有用,这就相当于是给了你一个二维数组,余下你就可以想怎么干就怎么干了。得益于这个十分好用的库代码很是简洁。但是还是有若干坑的存在导致话了一定时间探索。现在列出来供后人参考吧:
1、首先就是我的统计是根据姓名统计各个表中的信息的,但是调试发现不同的表中各个名字貌似不能够匹配,开始怀疑过编码问题,不过后来发现是因为空格。因为在excel中输入的时候很可能会顺手在一些名字后面加上几个空格或是tab键,这样看起来没什么差别,但是程序处理的时候这就是两个完全不同的串了。我的解决方法是给每个获取的字符串都加上strip()处理一下。效果良好
2、还是字符串的匹配,在判断某个单元格中的字符串(中文)是否等于我所给出的的时候发现无法匹配,并且各种unicode也不太奏效,百度过一些解决方案,但是都比较复杂或是没用。最后我采用了一个比较变通的方式:直接从excel中获取我想要的值再进行比较,效果是不错就是通用行不太好,个呢不能问题还没解决。
二、写excel表
写excel表要用到xlwt模块,官网下载(http://pypi.python.org/pypi/xlwt)。大致使用流程如下:
1、导入模块
importxlwt
2、创建workbook(其实就是excel,后来保存一下就行)
workbook=xlwt.Workbook(encoding='ascii')
3、创建表
worksheet=workbook.add_sheet('MyWorksheet')
4、往单元格内写入内容
worksheet.write(0,0,label='Row0,Column0Value')
5、保存
workbook.save('Excel_Workbook.xls')
由于我的需求比较简单,所以这上面没遇到什么问题,唯一的就是建议还是用ascii编码,不然可能会有一些诡异的现象。
当然xlwt功能远远不止这些,他甚至可以设置各种样式之类的。附上一点例子
ExamplesGeneratingExcelDocumentsUsingPython'sxlwt
HerearesomesimpleexamplesusingPython'sxlwtlibrarytodynamicallygenerateExceldocuments.
Pleasenoteausefulalternativemaybeezodf,whichallowsyoutogenerateODS(OpenDocumentSpreadsheet)filesforLibreOffice/OpenOffice.Youcancheckthemoutat:http://packages.python.org/ezodf/index.html
TheSimplestExample importxlwt workbook=xlwt.Workbook(encoding='ascii') worksheet=workbook.add_sheet('MyWorksheet') worksheet.write(0,0,label='Row0,Column0Value') workbook.save('Excel_Workbook.xls')
FormattingtheContentsofaCell importxlwt workbook=xlwt.Workbook(encoding='ascii') worksheet=workbook.add_sheet('MyWorksheet') font=xlwt.Font()#CreatetheFont font.name='TimesNewRoman' font.bold=True font.underline=True font.italic=True style=xlwt.XFStyle()#CreatetheStyle style.font=font#ApplytheFonttotheStyle worksheet.write(0,0,label='Unformattedvalue') worksheet.write(1,0,label='Formattedvalue',style)#ApplytheStyletotheCell workbook.save('Excel_Workbook.xls')
AttributesoftheFontObject font.bold=True#Maybe:True,False font.italic=True#Maybe:True,False font.struck_out=True#Maybe:True,False font.underline=xlwt.Font.UNDERLINE_SINGLE#Maybe:UNDERLINE_NONE,UNDERLINE_SINGLE,UNDERLINE_SINGLE_ACC,UNDERLINE_DOUBLE,UNDERLINE_DOUBLE_ACC font.escapement=xlwt.Font.ESCAPEMENT_SUPERSCRIPT#Maybe:ESCAPEMENT_NONE,ESCAPEMENT_SUPERSCRIPT,ESCAPEMENT_SUBSCRIPT font.family=xlwt.Font.FAMILY_ROMAN#Maybe:FAMILY_NONE,FAMILY_ROMAN,FAMILY_SWISS,FAMILY_MODERN,FAMILY_SCRIPT,FAMILY_DECORATIVE font.charset=xlwt.Font.CHARSET_ANSI_LATIN#Maybe:CHARSET_ANSI_LATIN,CHARSET_SYS_DEFAULT,CHARSET_SYMBOL,CHARSET_APPLE_ROMAN,CHARSET_ANSI_JAP_SHIFT_JIS,CHARSET_ANSI_KOR_HANGUL,CHARSET_ANSI_KOR_JOHAB,CHARSET_ANSI_CHINESE_GBK,CHARSET_ANSI_CHINESE_BIG5,CHARSET_ANSI_GREEK,CHARSET_ANSI_TURKISH,CHARSET_ANSI_VIETNAMESE,CHARSET_ANSI_HEBREW,CHARSET_ANSI_ARABIC,CHARSET_ANSI_BALTIC,CHARSET_ANSI_CYRILLIC,CHARSET_ANSI_THAI,CHARSET_ANSI_LATIN_II,CHARSET_OEM_LATIN_I font.colour_index=? font.get_biff_record=? font.height=0x00C8#C8inHex(indecimal)=10pointsinheight. font.name=? font.outline=? font.shadow=?
SettingtheWidthofaCell importxltw workbook=xlwt.Workbook() worksheet=workbook.add_sheet('MySheet') worksheet.write(0,0,'MyCellContents') worksheet.col(0).width=3333#3333=1"(oneinch). workbook.save('Excel_Workbook.xls')
EnteringaDateintoaCell importxlwt importdatetime workbook=xlwt.Workbook() worksheet=workbook.add_sheet('MySheet') style=xlwt.XFStyle() style.num_format_str='M/D/YY'#Otheroptions:D-MMM-YY,D-MMM,MMM-YY,h:mm,h:mm:ss,h:mm,h:mm:ss,M/D/YYh:mm,mm:ss,[h]:mm:ss,mm:ss.0 worksheet.write(0,0,datetime.datetime.now(),style) workbook.save('Excel_Workbook.xls')
AddingaFormulatoaCell importxlwt workbook=xlwt.Workbook() worksheet=workbook.add_sheet('MySheet') worksheet.write(0,0,5)#Outputs5 worksheet.write(0,1,2)#Outputs2 worksheet.write(1,0,xlwt.Formula('A1*B1'))#Shouldoutput"10"(A1[5]*A2[2]) worksheet.write(1,1,xlwt.Formula('SUM(A1,B1)'))#Shouldoutput"7"(A1[5]+A2[2]) workbook.save('Excel_Workbook.xls')
AddingaHyperlinktoaCell importxlwt workbook=xlwt.Workbook() worksheet=workbook.add_sheet('MySheet') worksheet.write(0,0,xlwt.Formula('HYPERLINK("http://www.google.com";"Google")'))#Outputsthetext"Google"linkingtohttp://www.google.com workbook.save('Excel_Workbook.xls')
MergingColumnsandRows importxlwt workbook=xlwt.Workbook() worksheet=workbook.add_sheet('MySheet') worksheet.write_merge(0,0,0,3,'FirstMerge')#Mergesrow0'scolumns0through3. font=xlwt.Font()#CreateFont font.bold=True#SetfonttoBold style=xlwt.XFStyle()#CreateStyle style.font=font#AddBoldFonttoStyle worksheet.write_merge(1,2,0,3,'SecondMerge',style)#Mergesrow1through2'scolumns0through3. workbook.save('Excel_Workbook.xls')
SettingtheAlignmentfortheContentsofaCell importxlwt workbook=xlwt.Workbook() worksheet=workbook.add_sheet('MySheet') alignment=xlwt.Alignment()#CreateAlignment alignment.horz=xlwt.Alignment.HORZ_CENTER#Maybe:HORZ_GENERAL,HORZ_LEFT,HORZ_CENTER,HORZ_RIGHT,HORZ_FILLED,HORZ_JUSTIFIED,HORZ_CENTER_ACROSS_SEL,HORZ_DISTRIBUTED alignment.vert=xlwt.Alignment.VERT_CENTER#Maybe:VERT_TOP,VERT_CENTER,VERT_BOTTOM,VERT_JUSTIFIED,VERT_DISTRIBUTED style=xlwt.XFStyle()#CreateStyle style.alignment=alignment#AddAlignmenttoStyle worksheet.write(0,0,'CellContents',style) workbook.save('Excel_Workbook.xls')
AddingBorderstoaCell #Pleasenote:WhileIwasabletofindtheseconstantswithinthesourcecode,onmysystem(usingLibreOffice,)Iwasonlypresentedwithasolidline,varyingfromthintothick;nodottedordashedlines. importxlwt workbook=xlwt.Workbook() worksheet=workbook.add_sheet('MySheet') borders=xlwt.Borders()#CreateBorders borders.left=xlwt.Borders.DASHED#Maybe:NO_LINE,THIN,MEDIUM,DASHED,DOTTED,THICK,DOUBLE,HAIR,MEDIUM_DASHED,THIN_DASH_DOTTED,MEDIUM_DASH_DOTTED,THIN_DASH_DOT_DOTTED,MEDIUM_DASH_DOT_DOTTED,SLANTED_MEDIUM_DASH_DOTTED,or0x00through0x0D. borders.right=xlwt.Borders.DASHED borders.top=xlwt.Borders.DASHED borders.bottom=xlwt.Borders.DASHED borders.left_colour=0x40 borders.right_colour=0x40 borders.top_colour=0x40 borders.bottom_colour=0x40 style=xlwt.XFStyle()#CreateStyle style.borders=borders#AddBorderstoStyle worksheet.write(0,0,'CellContents',style) workbook.save('Excel_Workbook.xls')
SettingtheBackgroundColorofaCell importxlwt workbook=xlwt.Workbook() worksheet=workbook.add_sheet('MySheet') pattern=xlwt.Pattern()#CreatethePattern pattern.pattern=xlwt.Pattern.SOLID_PATTERN#Maybe:NO_PATTERN,SOLID_PATTERN,or0x00through0x12 pattern.pattern_fore_colour=5#Maybe:8through63.0=Black,1=White,2=Red,3=Green,4=Blue,5=Yellow,6=Magenta,7=Cyan,16=Maroon,17=DarkGreen,18=DarkBlue,19=DarkYellow,almostbrown),20=DarkMagenta,21=Teal,22=LightGray,23=DarkGray,thelistgoeson... style=xlwt.XFStyle()#CreatethePattern style.pattern=pattern#AddPatterntoStyle worksheet.write(0,0,'CellContents',style) workbook.save('Excel_Workbook.xls')
TODO:ThingsLefttoDocument -Panes--separateviewswhicharealwaysinview -BorderColors(documentedabove,butnottakingeffectasitshould) -BorderWidths(documentabove,butnotworkingasexpected) -Protection -RowStyles -Zoom/Manification -WSProps? SourceCodeforreferenceavailableat:https://secure.simplistix.co.uk/svn/xlwt/trunk/xlwt/