excel in python

栏目: Python · 发布时间: 7年前

内容简介:mac 上的安装命令扩展:其他的安装方式参见

python-excel 是一组用 python 实现的 excel 文件访问库,包括 xlrdxlwtxlutils 三个模块。这里介绍一下它的基本用法,大约需要 10 分钟阅读。

安装

mac 上的安装命令

sudo pip install xlrd
sudo pip install xlwt
sudo pip install xlutils
复制代码

扩展:其他的安装方式参见 这里

读文件

读文件示例代码如下:

# -*- coding: utf-8 -*- 

import sys  
reload(sys)  
sys.setdefaultencoding('utf8')  
import xlrd

def read_file(file_path):
	book = xlrd.open_workbook(file_path) #得到 Excel 文件的 book 对象,实例化对象
	sheet = book.sheet_by_index(0) # 通过 sheet 索引获得 sheet 对象
	ncols = sheet.ncols # 列数
	nrows = sheet.nrows # 行数
	print("row count:{:d}, column count:{:d}".format(nrows, ncols))
	for row in range(nrows):
		print(sheet.row_values(row))

if __name__ == '__main__':
	read_file("test.xls")
复制代码

当然也可以逐个 cell 地读取文件,代码如下:

# -*- coding: utf-8 -*- 

from __future__ import print_function
import sys  
reload(sys)  
sys.setdefaultencoding('utf8')  
import xlrd

def read_file(file_path):
	book = xlrd.open_workbook(file_path)
	sheet = book.sheet_by_index(0)
	ncols = sheet.ncols
	nrows = sheet.nrows
	for row in range(nrows):
		for col in range(ncols):
			print(sheet.cell_value(row, col), end='')
			print('\t', end='')
		print("")

if __name__ == '__main__':
	read_file("test.xls")
复制代码

扩展:xlrd 的 Api 文档在这里。

写文件

写文件的示例代码如下:

# -*- coding: utf-8 -*- 
import sys  
reload(sys)  
sys.setdefaultencoding('utf8')  
import xlwt

def get_data():
	data = []
	data.append(["id", "value"])
	for i in range(10):
		col_data = []
		col_data.append(i)
		col_data.append(i * 10)
		data.append(col_data)
	return data

def write_file(file_path, sheet_name, data):
	book = xlwt.Workbook(encoding = 'utf-8')
	sheet = book.add_sheet(sheet_name)
	row = 0
	for row_data in data:
		col = 0
		for cell_data in row_data:
			sheet.write(row, col, cell_data)
			col += 1
		row += 1
	book.save(file_path)

if __name__ == '__main__':
	data = get_data()
	write_file("test.xls", "test sheet", data)
复制代码

扩展:xlwt 的 Api 文档在这里。

过滤文件示例

下面这个过滤文件的示例,是 excel 文件读写的综合应用:

# -*- coding: utf-8 -*- 

import sys  
reload(sys)  
sys.setdefaultencoding('utf8')  
import xlrd
import xlwt

# 过滤掉第二列元素等于 keyword 的数据所在行
def filter_file(src_file, src_sheet_name, desc_file, desc_sheet_name, keyword):
	src_book = xlrd.open_workbook(src_file)
	src_sheet = src_book.sheet_by_name(src_sheet_name)

	desc_book = xlwt.Workbook(encoding = 'utf-8')
	desc_sheet = desc_book.add_sheet(desc_sheet_name)

	ncols = src_sheet.ncols
	nrows = src_sheet.nrows
	index = 0
	for row in range(nrows):
		content = src_sheet.cell_value(row, 1)
		if content == keyword:
			continue
		for col in range(ncols):
			desc_sheet.write(index, col, src_sheet.cell_value(row, col))
		index += 1

	desc_book.save(desc_file)	

if __name__ == '__main__':
	filter_file("test.xls", "test sheet", "test_desc.xls", "test sheet", 30)
复制代码

上述例子中的 test.xls 文件内容为:

id value
0 0
1 10
3 30
2 20
4 40
5 50
6 60
7 70
8 80
9 90

以上所述就是小编给大家介绍的《excel in python》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Algorithms for Image Processing and Computer Vision

Algorithms for Image Processing and Computer Vision

Parker, J. R. / 2010-12 / 687.00元

A cookbook of algorithms for common image processing applications Thanks to advances in computer hardware and software, algorithms have been developed that support sophisticated image processing with......一起来看看 《Algorithms for Image Processing and Computer Vision》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具