Python中如何读取xls中的数据
要想读取EXCEL中的数据,首先得下载xlrd包,地址:https://pypi.python.org/pypi/xlrd 安装方法:下载解压后,利用windows dos命令进入解压目录eg,cd E:\selenium--Pyton学习\Python软件\xlwt-1.0.0\xlwt-1.0.0,接下来输入命令:python setup.py install 即可,python中读取EXCEL数据看如下代码:
#coding=utf-8
import xlrd
my_file='E:\\baidu.xlsx' book=xlrd.open_workbook(my_file)
print book.nsheets
print book.sheets()
print book.sheet_names() #sheet = book.sheet_by_index(0) #通过索引顺序获取
sheet = book.sheet_by_name(u'Sheet1')#通过名称获取
print sheet.nrows#获取该xls表中所用行数
print sheet.ncols#获取该xls表中所用列数
print sheet.name#获取该sheet名字
print sheet.row(1)#获取第一行
print sheet.row_values(1)#获取第一行值列表
print sheet.col(1)#获取第一列
print sheet.col_values(1)#获取第一列值列表 cell=sheet.cell(2,1)#获取第2行第1列的值
cell_value = sheet.cell_value(2,1)#获取第2行第1列的值
#cell_value = sheet.cell(2,1).value#获取第2行第1列的值
print cell_value
以下为向EXCEL中写入数据:
xlwt 地址:http://pypi.python.org/pypi/xlwt
创建一个Excel文件并创建一个Sheet:
1
2
3
4
|
from xlwt import * book = Workbook() sheet = book.add_sheet( 'Sheet1' ) book.save( 'myExcel.xls' ) |
Workbook类可以有encoding和style_compression参数。
encoding,设置字符编码,style_compression,表示是否压缩。这样设置:w = Workbook(encoding='utf-8'),就可以在excel中输出中文了。默认是ascii。
向sheet写入内容:
1
|
sheet.write(r, c, label = "", style = Style.default_style) |
简单写入:
1
|
sheet.write( 0 , 0 , label = 'Row 0, Column 0 Value' ) |
设置格式写入:
1
2
3
4
5
6
7
8
9
|
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' ) |
写入日期:
1
2
3
|
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) |
写入公式:
1
2
3
4
|
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]) |
写入链接:
1
|
sheet.write( 0 , 0 , xlwt.Formula( 'HYPERLINK("http://www.google.com";"Google")' )) #输出 "Google"链接到http://www.google.com |
Python中如何读取xls中的数据的更多相关文章
- Python实现随机读取文本N行数据
工作中需要判断某个文本中的URL是否能正常访问,并且随机获取其中N行能正常访问的URL数据,我的思路是:读取文本每一行数据,用urlopen访问,将返回状态码为200的URL保存到一个列表,获得列表长 ...
- Java工程中如何读取配置文件中参数信息
Java中读取配置文件中参数: 方法一:通过JDK中Properties来实现对配置文件的读取. Properties主要用于读取Java的配置文件,不同的编程语言有自己所支持的配置文件,配置文件中很 ...
- python按行读取apk中版本号、包名等信息
主要是读apk中manifest.xml中的信息. 读单一apk信息:见“文件”中“apkInfo.xml”.实际运行时,需要将后缀由“.xml”改为“.py". 批量自动获取apk软件信息 ...
- python 利用split读取文本文件中每一行的数字并保存至相应文件夹
import re from numpy import * def getStr(file_path,file_path1): fp = open(file_path, 'r') op = open( ...
- python新建一个表格xls并写入数据
# -*- coding:utf-8 -*- import xlwt workbook = xlwt.Workbook() # 新建一个工作簿 sheet = workbook.add_sheet(& ...
- 2)PHP中把读取.txt中内容并转为UTF-8格式
<?php $filename = "filename.txt"; $handle = fopen($filename, "r");//读取二进制文件时, ...
- java读取xls和xlsx数据作为数据驱动来用
java读取Excle代码 拿来可以直接使用 :针对xls 和 xlsx package dataProvider; import java.io.File; import java.io.FileI ...
- C++中遍历读取数组中的元素
答案来源:https://zhidao.baidu.com/question/187071815.html 对于字符数组str[N],判断方法有以下三种: 第一种:用库函数strlen 1 len = ...
- java中使用jxl读取excel中的数据
package bboss; import java.io.File; import java.io.FileInputStream; import java.io.IOException; impo ...
随机推荐
- Google在三大系统上停止对Chrome Apps的支持
近年来凭借着低廉的价格和易于管理和追踪的特性,Chrome OS设备逐渐获得了市场的肯定.只是相比较Windows和macOS桌面系统来说,Chrome OS在应用方面依然存在劣势,为此三年前Goog ...
- 解决Eclipse中文乱码
http://hsj69106.blog.51cto.com/1017401/595598 使用Eclipse编辑文件经常出现中文乱码或者文件中有中文不能保存的问题,Eclipse提供了灵活的设置文件 ...
- TimeVal类——Live555源码阅读(一)基本组件类
这是Live555源码阅读的第一部分,包括了时间类,延时队列类,处理程序描述类,哈希表类这四个大类. 这里是时间相关类的第一个部分. TimeVal类 TimeVal类定义在live555source ...
- Ext 下拉列表模糊搜索
/** * Created by huangbaidong on 2016/9/18. * 楼盘通用Combo组件,支持模糊查询 * 使用案例: * { fieldLabel : '楼盘名称', xt ...
- Git SourceTree 冲突解决方案
Git现在越来越火,很多人都从Svn迁移到Git上面,Git让我们更加与世界接轨,不再是"局域网"的程序猿,特别是掌握了Git之后,会发现它真的很好用,本文对Git中比较烦人的冲突 ...
- Oracle: SQL组合不同字段作为一个查询条件
前端程序传过来的值是有三个字段组合之后的结果,后端程序处理,并且将查询的数据反馈给前端. PS:不能直接使用字段RPT_NO的,因为在这条记录中RPT_NO恰好等于其他三个字段的组合值. 正确的做法是 ...
- jQuery之元素筛选
1.eq() 筛选指定索引号的元素2.first() 筛选出第一个匹配的元素3.last() 筛选出最后一个匹配的元素4.hasClass() 检查匹配的元素是否含有指定的类5.filter() ...
- Range Sum Query 2D - Mutable & Immutable
Range Sum Query 2D - Mutable Given a 2D matrix matrix, find the sum of the elements inside the recta ...
- C#之显示效果
窗体最大化(包含任务栏): this.TopMost = true; , ); this.Size = Screen.PrimaryScreen.Bounds.Size; 窗体最大化(不包含任务栏): ...
- selenium源码分析-webdriver(二)
最近比较空闲就仔细看了一下Selenium的源码,因为主要是使用WebDriver所以重点关注了一下WebDriver的工作原理.在前一篇blog里已经解释过了WebDriver与之前Selenium ...