python xlrd,xlwt 读写excel文件
python 读excel文件,需要xlrd库。下载地址:https://pypi.python.org/pypi/xlrd
python 写excel文件,需要xlwt库。下载地址:https://pypi.python.org/pypi/xlwt/1.1.2
下载后修改扩展名为rar, 解压后安装:

安装成功后就可以引用了。如下代码:
# -*- coding: utf-8 -*-
import os
import xlrd
import xlwt
import datetime
##################读excel文件##############################
#打开Excel文件,参数:excelFile:Excel文件路径
def open_Excel(excelFile):
excelFile = unicode(excelFile, "utf8")
if os.path.isfile(excelFile):
try:
data = xlrd.open_workbook(excelFile)
return data
except Exception,e:
print str(e)
'''往EXCEl单元格写内容,每次写一行sheet:页签名称;row:行内容列表;rowIndex:行索引;
isBold:true:粗字段,false:普通字体'''
def WriteSheetRow(sheet,rowValueList,rowIndex,isBold):
i = 0
style = xlwt.easyxf('font: bold 1') #粗字体
#style = xlwt.easyxf('font: bold 1, color red;') #红色字体
for svalue in rowValueList:
strValue = unicode(str(svalue),'utf-8')
if isBold:
sheet.write(rowIndex,i,strValue,style)
else:
sheet.write(rowIndex,i,strValue)
i = i + 1
#根据索引获取Excel表格中的数据 参数:excelFile:Excel文件路径 ,by_index:表的索引
def open_Excel_ByIndex(excelFile,sheetIndex):
data = open_Excel(excelFile)
table = data.sheets()[sheetIndex]
nrows = table.nrows #行数
ncols = table.ncols #列数
cursor = getSqlCursor()
for i in xrange(0,nrows):
headCols = table.row_values(i) #某一行数据
for a in headCols:
print a
#测试
open_Excel_ByIndex("D:\\test.xlsx",0)
#根据名称获取Excel表格中的数据 参数:excelFile:Excel文件路径
#sheetName:Sheet1名称
def open_Excel_BySheetName(excelFile,sheetName):
sheetName = unicode(sheetName, "utf8")
data = open_Excel(excelFile)
table = data.sheet_by_name(sheetName)
nrows = table.nrows #行数
ncols = table.ncols #列数
cursor = getSqlCursor()
for i in xrange(0,nrows):
headCols = table.row_values(i) #某一行数据
for a in headCols:
print a
#测试
open_Excel_BySheetName("D:\\test.xlsx",'sheet1')
##################写excel文件##############################
'''写excel文件'''
def save_Excel(strFile):
excelFile = unicode(strFile, "utf8")
wbk = xlwt.Workbook()
sheet = wbk.add_sheet('sheet1')
headList = ['标题1','标题2','标题3','标题4']
rowIndex = 0
WriteSheetRow(sheet,headList,rowIndex,,True)
for i in xrange(1,11):
rowIndex = rowIndex + 1
valueList = []
for j in xrange(1,5):
valueList.append(j*i)
WriteSheetRow(sheet,valueList,rowIndex,False)
wbk.save(excelFile)
#测试
save_Excel("D:\\test.xlsx")
结果如下:

##################单元格常用设置##############################
1、设置超链接
#设置超链接
font = xlwt.Font() # Create Font
font.colour_index = 4 # 蓝色字体
font.underline=True
style = xlwt.XFStyle()
style.font = font
sheet.write(rowIndex,4,xlwt.Formula('HYPERLINK("https://www.baidu.com";"baidu")'),style)
2、设置单元格背景色
#设置单元格背景色
pattern = xlwt.Pattern()
pattern.pattern = xlwt.Pattern.SOLID_PATTERN
pattern.pattern_fore_colour = 5 #黄色
style = xlwt.XFStyle()
style.pattern = pattern
sheet.write(5, 5, 'Cell Contents', style)
''' 颜色值
0 = Black, 1 = White, 2 = Red, 3 = Green, 4 = Blue,
5 = Yellow, 6 = Magenta, 7 = Cyan, 16 = Maroon,
17 = Dark Green, 18 = Dark Blue, 19 = Dark Yellow ,
20 = Dark Magenta, 21 = Teal,
22 = Light Gray, 23 = Dark Gray
'''
3、设置粗字体
style = xlwt.easyxf('font: bold 1')
sheet.write(5, 5, 'Cell Contents', style)
4、设置字体颜色
style = xlwt.easyxf('font: bold 0, color red;')#红色字体
sheet.write(5, 5, 'Cell Contents', style)
5、设置列宽带
sheet.col(1).width = 3333 # 3333 = 1" (one inch)
6、设置日期格式
style = xlwt.XFStyle()
style.num_format_str = 'YYYY/MM/DD h:mm:ss'
sheet.write(5, 5, datetime.datetime.now(), style)

#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
7、合并行和列
sheet.write_merge(0, 0, 0, 3, 'First Merge')
sheet.write_merge(2, 4, 0, 3, 'Second Merge')

8、给单元格增加边框
borders = xlwt.Borders() # Create Borders
borders.left = xlwt.Borders.DASHED
# May be: NO_LINE, THIN, MEDIUM, DASHED, DOTTED, THICK, DOUBLE, HAIR, MEDIUM_DASHED, THIN_DASH_DOTTED, #MEDIUM_DASH_DOTTED, THIN_DASH_DOT_DOTTED, MEDIUM_DASH_DOT_DOTTED,
#SLANTED_MEDIUM_DASH_DOTTED, or 0x00 through 0x0D.
borders.right = xlwt.Borders.DASHED
borders.top = xlwt.Borders.DASHED
borders.bottom = xlwt.Borders.DASHED
borders.left_colour = 0x40
borders.right_colour = 0x40
borders.top_colour = 0x40
borders.bottom_colour = 0x40
style = xlwt.XFStyle() # Create Style
style.borders = borders # Add Borders to Style

9、设置单元格中内容中位置,居中,局左右等
alignment = xlwt.Alignment() # Create Alignment
# May be: HORZ_GENERAL, HORZ_LEFT, HORZ_CENTER, HORZ_RIGHT,
#HORZ_FILLED, HORZ_JUSTIFIED, HORZ_CENTER_ACROSS_SEL, HORZ_DISTRIBUTED
alignment.horz = xlwt.Alignment.HORZ_CENTER #水平居中
# May be: VERT_TOP, VERT_CENTER, VERT_BOTTOM, VERT_JUSTIFIED, VERT_DISTRIBUTED
alignment.vert = xlwt.Alignment.VERT_CENTER #垂直居中
style = xlwt.XFStyle() # Create Style
style.alignment = alignment # Add Alignment to Style
sheet.write(5, 5, 'Cell Contents', style)

python xlrd,xlwt 读写excel文件的更多相关文章
- 【转发】Python使用openpyxl读写excel文件
Python使用openpyxl读写excel文件 这是一个第三方库,可以处理xlsx格式的Excel文件.pip install openpyxl安装.如果使用Aanconda,应该自带了. 读取E ...
- python使用xlrd模块读写Excel文件的方法
本文实例讲述了python使用xlrd模块读写Excel文件的方法.分享给大家供大家参考.具体如下: 一.安装xlrd模块 到python官网下载http://pypi.python.org/pypi ...
- Python使用openpyxl读写excel文件
Python使用openpyxl读写excel文件 这是一个第三方库,可以处理xlsx格式的Excel文件.pip install openpyxl安装.如果使用Aanconda,应该自带了. 读取E ...
- python使用xlrd和xlwt读写Excel文件
版权声明:本文为博主原创文章,未经允许不得转载. 安装模块 如果使用的是Linux系统,并且安装了pip,可以直接使用pip安装xlrd, xlwt: pip install xlwt pip ins ...
- 自己总结python用xlrd\xlwt读写excel
1.首先安装xlrd\xlwt模块 xlrd模块下载地址: https://pypi.python.org/pypi/xlrd xlwt模块下载地址: https://pypi.python.org/ ...
- Python使用读写excel文件
Python使用openpyxl读写excel文件 这是一个第三方库,可以处理xlsx格式的Excel文件.pip install openpyxl安装.如果使用Aanconda,应该自带了. 读取E ...
- python读写Excel文件的函数--使用xlrd/xlwt
python中读取Excel的模块或者说工具有很多,如以下几种: Packages 文档下载 说明 openpyxl Download | Documentation | Bitbucket The ...
- python读写Excel文件--使用xlrd模块读取,xlwt模块写入
一.安装xlrd模块和xlwt模块 1. 下载xlrd模块和xlwt模块 到python官网http://pypi.python.org/pypi/xlrd下载模块.下载的文件例如:xlrd-0.9. ...
- Python -- xlrd,xlwt,xlutils 读写同一个Excel
最近开始学习python,想做做简单的自动化测试,需要读写excel,然后就找到了xlrd来读取Excel文件,使用xlwt来生成Excel文件(可以控制Excel中单元格的格式),需要注意的是,用x ...
随机推荐
- 对于 NSLayoutConstraint 不执行动画的处理:
在开发中 我们有时候需要改变某个空间的约束条件 也就是更改NSLayoutConstraint的值 (比如说我想在键盘顶部增加一个工具栏 让工具栏随着键盘的位置变化而变化 有一个动画效果)但是发 ...
- Python 基础【第八篇】变量
1.变量定义: 给数据进行命名,数据的名字就叫做变量 2.变量格式: [变量名] = [值] 注:变量名命名需要满足下面两条准则 准则一:标示符开头不能为数字.不能包含空格.特殊字符准则二:标示符不能 ...
- PV模型
你想建设一个能承受500万PV/每天的网站吗? 500万PV是什么概念?服务器每秒要处理多少个请求才能应对?如果计算呢? 一.PV是什么 PV是page view的简写.PV是指页面的访问次数,每打开 ...
- git 配置用户名和邮箱
在安装了git for windows之后,个人总是忘记配置git config的命令,以此记录一下: 配置用户名和邮箱的命令 git config --global user.name " ...
- [转载]ubuntu Atheros Communications Device 1083 驱动
Ubuntu 版本: Ubuntu server 10.10 在2016-03-26 上午时,拆开公司一台server电脑的CPU风扇不转,电源都烧掉了(潮湿的原因)... 在2016-03-28 打 ...
- 使用adb devices命令,老是报error:device offline的错误。
刚开始报error:devices not found 重新安装adb 驱动. 解决方法: adb kill-server adb start-server adb remount 再使用adb de ...
- 用JQuery编写textarea,input,checkbox,select
今天学习怎样用JQuery编写一些小的代码,小小的试了一下编写一个textarea,代码如下: <!DOCTYPE HTML> <html lang="en"&g ...
- javascript 第26节 jQuery对象
<html> <head> <title>jQuery</title> <!--导入jquery库--> <script type=& ...
- C++中map用法
/************************************************************************** Map的特点: 1.存储Key-value对* ...
- makefile--Unfound symbol
Unfound symbol ,库函数的包含问题或者头文件包含问题 makefile对#的识别度太低了,如果使用了,#它之后的可能就不能识别了,然后会报错的Unfound symbol /////// ...