600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > python处理excel实例编程_python处理Excel的简单示例

python处理excel实例编程_python处理Excel的简单示例

时间:2023-09-22 12:31:13

相关推荐

python处理excel实例编程_python处理Excel的简单示例

对python这个高级语言感兴趣的小伙伴,下面一起跟随编程之家 的小编两巴掌来看看吧!

Python中一般使用xlrd库来读取Excel文件,使用xlwt库来生成Excel文件,使用xlutils库复制和修改Excel文件。这三个库只支持到Excel。

python-excel主页地址:http://www.python-/

xlrd

地址:/pypi/xlrd

github地址:/python-excel/xlrd

打开excel文件,获取一个Book()对象:

# @param python处理Excel

# @author 编程之家 |

import xlrd

book = xlrd.open_workbook("myfile.xls")

# End

获取sheets数目:

# @param python处理Excel

# @author 编程之家 |

>>> book.nsheets

3

# End

获取sheets列表:

# @param python处理Excel

# @author 编程之家 |

>>> book.sheets()

[,,]

# End

获取sheets name列表:

# @param python处理Excel

# @author 编程之家 |

>>> book.sheet_names()

[u'Sheet1',u'Sheet2',u'Sheet3']

# End

获取Book()中的Sheet:

# @param python处理Excel

# @author 编程之家 |

sheet = book.sheets()[0] #sheets返回一个sheet列表

sheet = book.sheet_by_index(0) #通过索引顺序获取

sheet = book.sheet_by_name(u'Sheet1')#通过名称获取

# End

获取行数,列数,名字:

# @param python处理Excel

# @author 编程之家 |

>>> sheet.nrows

1002

>>> sheet.ncols

11

>>> sheet.name

u'Sheet1'

# End

获取某行,某行值列表,某列,某列值列表:

# @param python处理Excel

# @author 编程之家 |

sheet.row(i)

sheet.row_values(i)

sheet.col(i)

sheet.col_values(i)

# End

获取单元格的值:

# @param python处理Excel

# @author 编程之家 |

cell = sheet.cell(i,j)

cell_value = sheet.cell_value(i,j)

cell_value = sheet.cell(i,j).value

# End

需要注意的是,用xlrd读取excel是不能对其进行操作的:xlrd.open_workbook()方法返回xlrd.Book类型,是只读的,不能对其进行操作。

xlwt

地址:/pypi/xlwt,适用于python2.3-2.7

xlwt-future:/pypi/xlwt-future/0.8.0,适用于Python 2.6-3.3

github地址:/python-excel/xlwt

创建一个Excel文件并创建一个Sheet:

# @param python处理Excel

# @author 编程之家 |

from xlwt import *

book = Workbook()

sheet = book.add_sheet('Sheet1')

book.save('myExcel.xls')

# End

Workbook类可以有encoding和style_compression参数。

encoding,设置字符编码,style_compression,表示是否压缩。这样设置:w = Workbook(encoding='utf-8'),就可以在excel中输出中文了。默认是ascii。

向sheet写入内容:

# @param python处理Excel

# @author 编程之家 |

sheet.write(r,c,label="",style=Style.default_style)

# End

简单写入:

# @param python处理Excel

# @author 编程之家 |

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

# End

设置格式写入:

# @param python处理Excel

# @author 编程之家 |

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,label = 'Formatted value',style) # Apply the Style to the Cell

book.save('myExcel.xls')

# End

写入日期:

# @param python处理Excel

# @author 编程之家 |

style = xlwt.XFStyle()

style.num_format_str = 'M/D/YY' # Other options: D-MMM-YY,D-MMM,MMM-YY,h:mm,h:mm:ss,M/D/YY h:mm,mm:ss,[h]:mm:ss,mm:ss.0

sheet.write(0,datetime.datetime.now(),style)

# End

写入公式:

# @param python处理Excel

# @author 编程之家 |

sheet.write(0,5) # Outputs 5

sheet.write(0,1,2) # Outputs 2

sheet.write(1,xlwt.Formula('A1*B1')) # 输出 "10" (A1[5] * A2[2])

sheet.write(1,xlwt.Formula('SUM(A1,B1)')) # 输出 "7" (A1[5] + A2[2])

# End

写入链接:

# @param python处理Excel

# @author 编程之家 |

sheet.write(0,xlwt.Formula('HYPERLINK("";"Google")')) #输出 "Google"链接到

# End

xlutils

地址:/xlutils/

github地址:/python-excel/xlutils

xlutils.copy.copy(wb)

复制一个xlrd.Book对象,生成一个xlwt.Workbook对象,可以对xlwt.Workbook进行修改。

# @param python处理Excel

# @author 编程之家 |

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,'value')

wb.save('myExcel.xls')

# End

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。