≡
  • 网络编程
  • 数据库
  • CMS技巧
  • 软件编程
  • PHP笔记
  • JavaScript
  • MySQL
位置:首页 > 网络编程 > Python

python处理Excel的简单示例

人气:1156 时间:2018-09-30

这篇文章主要为大家详细介绍了python处理Excel的简单示例,具有一定的参考价值,可以用来参考一下。

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

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()对象:


# @param python处理Excel
# @author 四海网|q1010.com 

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

# End www_512pic_com

获取sheets数目:


# @param python处理Excel
# @author 四海网|q1010.com 

>>> book.nsheets
3

# End www_512pic_com

获取sheets列表:


# @param python处理Excel
# @author 四海网|q1010.com 

>>> book.sheets()
[<xlrd.sheet.Sheet object at 0x01A93970>, <xlrd.sheet.Sheet object at 0x01A93950>, <xlrd.sheet.Sheet object at 0x01A93E70>]

# End www_512pic_com

获取sheets name列表:


# @param python处理Excel
# @author 四海网|q1010.com 

>>> book.sheet_names()
[u'Sheet1', u'Sheet2', u'Sheet3']

# End www_512pic_com

获取Book()中的Sheet:


# @param python处理Excel
# @author 四海网|q1010.com 

sheet = book.sheets()[0]          #sheets返回一个sheet列表
sheet = book.sheet_by_index(0)    #通过索引顺序获取
sheet = book.sheet_by_name(u'Sheet1')#通过名称获取

# End www_512pic_com

获取行数,列数,名字:


# @param python处理Excel
# @author 四海网|q1010.com 

>>> sheet.nrows
1002
>>> sheet.ncols
11
>>> sheet.name
u'Sheet1'

# End www_512pic_com

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


# @param python处理Excel
# @author 四海网|q1010.com 

sheet.row(i)
sheet.row_values(i)
sheet.col(i)
sheet.col_values(i)

# End www_512pic_com

获取单元格的值:


# @param python处理Excel
# @author 四海网|q1010.com 

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

# End www_512pic_com

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

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:


# @param python处理Excel
# @author 四海网|q1010.com 

from xlwt import *
book = Workbook()
sheet = book.add_sheet('Sheet1')
book.save('myExcel.xls')

# End www_512pic_com

Workbook类可以有encoding和style_compression参数。

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

向sheet写入内容:


# @param python处理Excel
# @author 四海网|q1010.com 

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

# End www_512pic_com

简单写入:


# @param python处理Excel
# @author 四海网|q1010.com 

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

# End www_512pic_com

设置格式写入:


# @param python处理Excel
# @author 四海网|q1010.com 

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

# End www_512pic_com

写入日期:


# @param python处理Excel
# @author 四海网|q1010.com 

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)

# End www_512pic_com

写入公式:


# @param python处理Excel
# @author 四海网|q1010.com 

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])

# End www_512pic_com

写入链接:


# @param python处理Excel
# @author 四海网|q1010.com 

sheet.write(0, 0, xlwt.Formula('HYPERLINK("http://www.google.com";"Google")')) #输出 "Google"链接到http://www.google.com

# End www_512pic_com

xlutils

地址:http://pythonhosted.org/xlutils/

github地址:https://github.com/python-excel/xlutils

xlutils.copy.copy(wb)

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


# @param python处理Excel
# @author 四海网|q1010.com 

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

# End www_512pic_com

本文来自:http://www.q1010.com/181/1941-0.html

注:关于python处理Excel的简单示例的内容就先介绍到这里,更多相关文章的可以留意四海网的其他信息。

关键词:Excel

您可能感兴趣的文章

  • PHP读取Excel文件的简单示例
  • php 在网页生成excel提供下载示例
  • php 将mysql数据转换到excel输出的简单示例
  • php 通过com连接excel并输出数据示例
  • php 操作Excel工具PhpExcel用法入门实例
  • php 生成excel文档的简单示例
  • php mysql数据导出到excel文件
上一篇:python 单引号,双引号,多引号的简单示例
下一篇:python数据格式化pprint的简单示例
热门文章
  • Python 处理Cookie的菜鸟教程(一)Cookie库
  • python之pandas取dataframe特定行列的简单示例
  • Python解决json.dumps错误::‘utf8’ codec can‘t decode byte
  • Python通过pythony连接Hive执行Hql的脚本
  • Python 三种方法删除列表中重复元素的简单示例
  • python爬虫代码示例
  • Python 中英文标点转换示例
  • Python 不得不知的开源项目解析
  • Python urlencode编码和url拼接实现方法
  • python按中文拆分中英文混合字符串的简单示例
  • 最新文章
    • Python利用numpy三层神经网络的简单示例
    • pygame可视化幸运大转盘的简单示例
    • Python爬虫之爬取二手房信息的简单示例
    • Python之time库的简单示例
    • OpenCV灰度、高斯模糊、边缘检测的简单示例
    • Python安装Bs4及使用的简单示例
    • django自定义manage.py管理命令的简单示例
    • Python之matplotlib 向任意位置添加一个子图(axes)的简单示例
    • Python图像标签标注软件labelme分析的简单示例
    • python调用摄像头并拍照发邮箱的简单示例

四海网收集整理一些常用的php代码,JS代码,数据库mysql等技术文章。