您的位置:首页 > 编程语言 > Python开发

【Python】操作excel 进行读写

2016-10-26 10:05 507 查看
参考文章

http://www.cnblogs.com/BeginMan/p/3657805.html
http://www.cnblogs.com/lhj588/archive/2012/01/06/2314181.html
http://www.jb51.net/article/77626.htm
Python读写Excel文件的实例
python中使用xlrd、xlwt操作excel表格详解
用python读写excel的方法
用Python的pandas框架操作Excel文件中的数据教程
python抓取某汽车网数据解析html存入excel示例
用python + openpyxl处理excel2007文档思路以及心得
python使用xlrd模块读写Excel文件的方法
Python GAE、Django导出Excel的方法
Python修改Excel数据的实例代码
python启动办公软件进程(word、excel、ppt、以及wps的et、wps、wpp)
Python中使用第三方库xlrd来写入Excel文件示例
Windows下Python使用Pandas模块操作Excel文件的教程

正文:

操作excel的模块:

openpyxl

The recommended package for reading and writing Excel 2010 files (ie: .xlsx)

Download | Documentation | Bitbucket

xlsxwriter

An alternative package for writing data, formatting information and, in particular, charts in the Excel 2010 format (ie: .xlsx)

Download | Documentation | GitHub

xlrd

This package is for reading data and formatting information from older Excel files (ie: .xls)

Download |Documentation |GitHub

xlwt

This package is for writing data and formatting information to older Excel files (ie: .xls)

Download | Documentation |Examples |
GitHub

xlutils

This package collects utilities that require both
xlrd and xlwt, including the ability to copy and modify or filter existing excel files.
NB: In general, these use cases are now covered by openpyxl!

Download | Documentation |GitHub

说明:
xlrd模块主要用于读取Excel表

xlwt与xlsxwriter模块主要用于将数据写入表中,但是xlwt与xlsxwriter模块不支持修改表

例子1:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 读取excel数据
import xlrd
import xlwt
temp_data1 = []
temp_data2 = []
temp_data3 = []
data1=xlrd.open_workbook("D:\第一个测试文件.xlsx") # 打开xlsx文件
data2=xlrd.open_workbook("D:\第二个测试文件.xlsx") # 打开xlsx文件
data3=xlrd.open_workbook("D:\第三个测试文件.xlsx") # 打开xlsx文件
table1=data1.sheets()[0] # 打开第一张表
table2=data2.sheets()[0] # 打开第二张表
table3=data3.sheets()[0] # 打开第三张表
nrows1=table1.nrows # 获取表的行数
nrows2=table2.nrows # 获取表的行数
nrows3=table3.nrows # 获取表的行数
for i in range(nrows1): # 循环逐行打印
    temp_data1.append(table1.row_values(i)[:nrows1])
    #print table.row_values(i)[:nrows1] # 取nrows1列

for i in range(nrows2): # 循环逐行打印
    temp_data1.append(table2.row_values(i)[:nrows2])

for i in range(nrows3): # 循环逐行打印
    temp_data1.append(table3.row_values(i)[:nrows3])

print(len(temp_data1))
#print(len(temp_data2))
#print(len(temp_data3))

filename = xlwt.Workbook ()
sheet = filename.add_sheet('name', cell_overwrite_ok=True)
for i in range(len(temp_data1)):
    #print(temp_data1[i])
    sheet.write(i,0,temp_data1[i][0])
    sheet.write(i,1,temp_data1[i][1])

filename.save('test.xls')
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: