python处理Excel
Python中一般使用xlrd库来读取Excel文件,使用xlwt库来生成Excel文件,使用xlutils库复制和修改Excel文件。这三个库只支持到Excel2003。
python-excel主页地址:http://www.python-excel.org/
xlrd
地址:https://pypi.python.org/pypi/xlrd
github地址:https://github.com/python-excel/xlrd
打开excel文件,获取一个Book()对象:
1 2 | import xlrd book = xlrd.open_workbook( "myfile.xls" ) |
获取sheets数目:
1 2 | >>> book.nsheets 3 |
获取sheets列表:
1 2 | >>> book.sheets() [<xlrd.sheet.Sheet object at 0x01A93970 >, <xlrd.sheet.Sheet object at 0x01A93950 >, <xlrd.sheet.Sheet object at 0x01A93E70 >] |
获取sheets name列表:
1 2 | >>> book.sheet_names() [u 'Sheet1' , u 'Sheet2' , u 'Sheet3' ] |
获取Book()中的Sheet:
1 2 3 | sheet = book.sheets()[ 0 ] #sheets返回一个sheet列表 sheet = book.sheet_by_index( 0 ) #通过索引顺序获取 sheet = book.sheet_by_name(u 'Sheet1' ) #通过名称获取 |
获取行数,列数,名字:
1 2 3 4 5 6 | >>> sheet.nrows 1002 >>> sheet.ncols 11 >>> sheet.name u 'Sheet1' |
获取某行,某行值列表,某列,某列值列表:
1 2 3 4 | sheet.row(i) sheet.row_values(i) sheet.col(i) sheet.col_values(i) |
获取单元格的值:
1 2 3 | cell = sheet.cell(i,j) cell_value = sheet.cell_value(i,j) cell_value = sheet.cell(i,j).value |
需要注意的是,用xlrd读取excel是不能对其进行操作的:xlrd.open_workbook()方法返回xlrd.Book类型,是只读的,不能对其进行操作。
xlrd读取Excel中的日期:
一个值为1984/5/30的单元格
1 2 3 4 5 6 7 8 | >>> print sheet.cell( 2 , 7 ) #直接读取是个日期格式,读value会返回30832.0的值 xldate: 30832.0 >>> xlrd.xldate_as_tuple(sheet.cell( 2 , 7 ).value, 0 ) #变为元祖,第二个参数有两种取值,0或者1,0是以1900-01-01为基准的日期,而1是1904-01-01为基准的日期。 ( 1984 , 5 , 30 , 0 , 0 , 0 ) >>> xlrd.xldate.xldate_as_datetime(sheet.cell( 2 , 7 ).value, 1 ) datetime.datetime( 1988 , 5 , 31 , 0 , 0 ) >>> xlrd.xldate.xldate_as_datetime(sheet.cell( 2 , 7 ).value, 0 ) datetime.datetime( 1984 , 5 , 30 , 0 , 0 ) |
xlwt
地址:http://pypi.python.org/pypi/xlwt,适用于python2.3-2.7
xlwt-future:https://pypi.python.org/pypi/xlwt-future/0.8.0,适用于Python 2.6-3.3
github地址:https://github.com/python-excel/xlwt
创建一个Excel文件并创建一个Sheet:
1 2 3 4 | from xlwt import * book = Workbook() sheet = book.add_sheet( 'Sheet1' ) book.save( 'myExcel.xls' ) |
Workbook类可以有encoding和style_compression参数。
encoding,设置字符编码,style_compression,表示是否压缩。这样设置:w = Workbook(encoding='utf-8'),就可以在excel中输出中文了。默认是ascii。
向sheet写入内容:
sheet.write(r, c, label="", style=Style.default_style)
简单写入:
1 | sheet.write( 0 , 0 , label = 'Row 0, Column 0 Value' ) |
设置格式写入:
1 2 3 4 5 6 7 8 9 | font = xlwt.Font() # 字体 font.name = 'Times New Roman' font.bold = True font.underline = True font.italic = True style = xlwt.XFStyle() # 创建一个格式 style.font = font # 设置格式字体 sheet.write( 1 , 0 , label = 'Formatted value' , style) # Apply the Style to the Cell book.save( 'myExcel.xls' ) |
写入日期:
1 2 3 | style = xlwt.XFStyle() style.num_format_str = 'M/D/YY' # Other options: D-MMM-YY, D-MMM, MMM-YY, h:mm, h:mm:ss, h:mm, h:mm:ss, M/D/YY h:mm, mm:ss, [h]:mm:ss, mm:ss.0 sheet.write( 0 , 0 , datetime.datetime.now(), style) |
写入公式:
1 2 3 4 | sheet.write( 0 , 0 , 5 ) # Outputs 5 sheet.write( 0 , 1 , 2 ) # Outputs 2 sheet.write( 1 , 0 , xlwt.Formula( 'A1*B1' )) # 输出 "10" (A1[5] * A2[2]) sheet.write( 1 , 1 , xlwt.Formula( 'SUM(A1,B1)' )) # 输出 "7" (A1[5] + A2[2]) |
写入链接:
1 | sheet.write( 0 , 0 , xlwt.Formula( 'HYPERLINK("http://www.google.com";"Google")' )) #输出 "Google"链接到http://www.google.com |
xlutils
地址:http://pythonhosted.org/xlutils/
github地址:https://github.com/python-excel/xlutils
xlutils.copy.copy(wb)
复制一个xlrd.Book对象,生成一个xlwt.Workbook对象,可以对xlwt.Workbook进行修改。
1 2 3 4 5 6 7 8 | from xlrd import open_workbook from xlutils.copy import copy book = open_workbook( 'myExcel.xls' ) wbook = copy(book) #wbook即为xlwt.WorkBook对象 wsheet = wbook.get_sheet( 0 ) #通过get_sheet()获取的sheet有write()方法 wsheet.write( 0 , 0 , 'value' ) wb.save( 'myExcel.xls' ) |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· winform 绘制太阳,地球,月球 运作规律
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· 写一个简单的SQL生成工具
· Manus的开源复刻OpenManus初探