A. python 处理excel
使用xlrd读取文件,使用xlwt生成Excel文件(可以控制Excel中单元格的格式)。但是用xlrd读取excel是不能对其进行操作的;而xlwt生成excel文件是不能在已有的excel文件基础上进行修改的,如需要修改文件就要使用xluntils模块。pyExcelerator模块与xlwt类似,也可以用来生成excel文件。
[代码]test_xlrd.py **
#coding=utf-8
#######################################################
#filename:test_xlrd.py
#author:defias
#date:xxxx-xx-xx
#function:读excel文件中的数据
#######################################################
import xlrd
#打开一个workbook
workbook = xlrd.open_workbook('E:\\Code\\Python\\testdata.xls')
#抓取所有sheet页的名称
worksheets = workbook.sheet_names()
print('worksheets is %s' %worksheets)
#定位到sheet1
worksheet1 = workbook.sheet_by_name(u'Sheet1')
"""
#通过索引顺序获取
worksheet1 = workbook.sheets()[0]
#或
worksheet1 = workbook.sheet_by_index(0)
"""
"""
#遍历所有sheet对象
for worksheet_name in worksheets:
worksheet = workbook.sheet_by_name(worksheet_name)
"""
#遍历sheet1中所有行row
num_rows = worksheet1.nrows
for curr_row in range(num_rows):
row = worksheet1.row_values(curr_row)
print('row%s is %s' %(curr_row,row))
#遍历sheet1中所有列col
num_cols = worksheet1.ncols
for curr_col in range(num_cols):
col = worksheet1.col_values(curr_col)
print('col%s is %s' %(curr_col,col))
#遍历sheet1中所有单元格cell
for rown in range(num_rows):
for coln in range(num_cols):
cell = worksheet1.cell_value(rown,coln)
print cell
"""
#其他写法:
cell = worksheet1.cell(rown,coln).value
print cell
#或
cell = worksheet1.row(rown)[coln].value
print cell
#或
cell = worksheet1.col(coln)[rown].value
print cell
#获取单元格中值的类型,类型 0 empty,1 string, 2 number, 3 date, 4 boolean, 5 error
cell_type = worksheet1.cell_type(rown,coln)
print cell_type
"""
**2. [代码]test_xlwt.py **
#coding=utf-8
#######################################################
#filename:test_xlwt.py
#author:defias
#date:xxxx-xx-xx
#function:新建excel文件并写入数据
#######################################################
import xlwt
#创建workbook和sheet对象
workbook = xlwt.Workbook() #注意Workbook的开头W要大写
sheet1 = workbook.add_sheet('sheet1',cell_overwrite_ok=True)
sheet2 = workbook.add_sheet('sheet2',cell_overwrite_ok=True)
#向sheet页中写入数据
sheet1.write(0,0,'this should overwrite1')
sheet1.write(0,1,'aaaaaaaaaaaa')
sheet2.write(0,0,'this should overwrite2')
sheet2.write(1,2,'bbbbbbbbbbbbb')
"""
#-----------使用样式-----------------------------------
#初始化样式
style = xlwt.XFStyle()
#为样式创建字体
font = xlwt.Font()
font.name = 'Times New Roman'
font.bold = True
#设置样式的字体
style.font = font
#使用样式
sheet.write(0,1,'some bold Times text',style)
"""
#保存该excel文件,有同名文件时直接覆盖
workbook.save('E:\\Code\\Python\\test2.xls')
print '创建excel文件完成!'
**3. [代码]test_xlutils.py **
#coding=utf-8
#######################################################
#filename:test_xlutils.py
#author:defias
#date:xxxx-xx-xx
#function:向excel文件中写入数据
#######################################################
import xlrd
import xlutils.
#打开一个workbook
rb = xlrd.open_workbook('E:\\Code\\Python\\test1.xls')
wb = xlutils..(rb)
#获取sheet对象,通过sheet_by_index()获取的sheet对象没有write()方法
ws = wb.get_sheet(0)
#写入数据
ws.write(1, 1, 'changed!')
#添加sheet页
wb.add_sheet('sheetnnn2',cell_overwrite_ok=True)
#利用保存时同名覆盖达到修改excel文件的目的,注意未被修改的内容保持不变
wb.save('E:\\Code\\Python\\test1.xls')
**4. [代码]test_pyExcelerator_read.py **
#coding=utf-8
#######################################################
#filename:test_pyExcelerator_read.py
#author:defias
#date:xxxx-xx-xx
#function:读excel文件中的数据
#######################################################
import pyExcelerator
#parse_xls返回一个列表,每项都是一个sheet页的数据。
#每项是一个二元组(表名,单元格数据)。其中单元格数据为一个字典,键值就是单元格的索引(i,j)。如果某个单元格无数据,那么就不存在这个值
sheets = pyExcelerator.parse_xls('E:\\Code\\Python\\testdata.xls')
print sheets
**5. [代码]test_pyExcelerator.py **
#coding=utf-8
#######################################################
#filename:test_pyExcelerator.py
#author:defias
#date:xxxx-xx-xx
#function:新建excel文件并写入数据
#######################################################
import pyExcelerator
#创建workbook和sheet对象
wb = pyExcelerator.Workbook()
ws = wb.add_sheet(u'第一页')
#设置样式
myfont = pyExcelerator.Font()
myfont.name = u'Times New Roman'
myfont.bold = True
mystyle = pyExcelerator.XFStyle()
mystyle.font = myfont
#写入数据,使用样式
ws.write(0,0,u'ni hao 帕索!',mystyle)
#保存该excel文件,有同名文件时直接覆盖
wb.save('E:\\Code\\Python\\mini.xls')
print '创建excel文件完成!'
B. python怎么处理excel
可以循环列表填充到excel的每一个单元格,也可以按行或者列直接填充列表
C. python如何对excel数据进行处理
在python语言中,可以使用xlrd和xlwt两个库操作excel。
在python语言中处理Excel的方法:
在python项目中,新建python文件,并依次导入xlrd和xlwt。
接着调用open_workbook()方法,打开一个excel文件
调用sheet_by_name()方法,读取文件的sheet页
如果是后面加了个s,sheet_names表示获取excel中所有的sheet页
利用sheets()方法加序号,可以获取某个sheet页对象
如果想要获取excel某个sheet页中记录的总数,使用nrows
在cell()中传入两个值,一个行一个列,然后value获取对应单元格的值
推荐:python视频教程以上就是小编分享的关于python如何对excel数据进行处理的详细内容希望对大家有所帮助,更多有关python教程请关注环球青藤其它相关文章!
D. phthon 怎么用for 循环excel 中一行的数字
随便创建个excel文件,里面存1,2,...,10到第一行。
是这个意思麽=。=?
E. python处理excel教程是什么
python处理excel教程:首先打开pycharm工具,创建python项目;然后新建python文件,依次导入openpyxl、xlrd和xlwt,并定义函数;接着向excel插入数据;最后调用函数加载数据即可。
python处理excel教程:
1、打开pycharm工具,创建一个python项目,并打开项目
2、在指定文件夹下,新建python文件,依次导入openpyxl、xlrd和xlwt
3、定义函数write_data,创建excel的sheet页,然后向excel插入数据
4、再定义设置excel文档格式样式函数setExcelStyle,传入几个参数
5、判断__name__是否等于__main__,调用函数write_data()
6、保存代码并运行python文件,查看是否生成sales.xlsx文件
7、使用openpyxl模块中的load_workbook()方法,加载sales.xlsx文件
8、获取对应sheet页,然后获取对应单元格的值
以上就是小编分享的关于python处理excel教程是什么的详细内容希望对大家有所帮助,更多有关python教程请关注环球青藤其它相关文章!
F. python对EXCEL的操作
1)根据sheet的sheet_by_index属性索引获取
2)根据sheet的sheet_by_name属性名字获取
4.获取指定sheet的名字、行数、列数
调用指定sheet的name、nrows、ncols
G. 如何用python操作excel
指定选取三列然后挑选出同时满足>=1或者同时<=-1的 将其所有数据存入新的csv表格中
程序如下:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date : 2014-04-10 21:47:56
# @Function: 指定选取三列然后挑选出同时满足>=1或者同时<=-1的 将其所有数据存入新的csv表格中
# @Author : BeginMan
import os
import string
import xlrd
import xlwt
def get_data():
"""获取excel数据源"""
file = r'C:\Users\Administrator\Desktop\pytool\xlrd\initial_log_data.xls' # 改成自己的路径
filepath = raw_input(u'请将xls文件路径粘贴进去,如果程序里已经指定了文件则按Enter键继续:')
is_valid = False # 验证文件
try:
filepath = [file, filepath][filepath != '']
print filepath
# 判断给出的路径是不是xls格式
if os.path.isfile(filepath):
filename = os.path.basename(filepath)
if filename.split('.')[1] == 'xls':
is_valid = True
data = None
if is_valid:
data = xlrd.open_workbook(filepath)
except Exception, e:
print u'你操作错误:%s' %e
return None
return data
def handle_data():
"""处理数据"""
data = get_data()
if data:
col_format = ['B', 'C', 'D'] # 指定的列
inp = raw_input(u'请选择指定的三列,用逗号分隔,默认的是B,C,D(英文逗号,不区分大小写),如果选择默认则按Enter键继续:\n')
try:
inp = inp.split(',')
col_format = [col_format,inp][len([i for i in inp if i in string.letters]) == 3]
col_format = [i.upper() for i in col_format] # 转换成大写
table = data.sheet_by_index(0) # 选取第一个工作区
nrows = table.nrows # 行数
ncols = table.ncols # 列数
str_upcase = [i for i in string.uppercase] # 所有大写字母
i_upcase = range(len(str_upcase)) # 对应的数字
ncols_dir = dict(zip(str_upcase,i_upcase)) # 格式成字典
col_index = [ncols_dir.get(i) for i in col_format] # 获取指定列所对应的索引
# 选取的三列是否同时满足 >=1或者同时<=-1
print u'正在检索中……'
count = 0
result = []
for i in xrange(nrows):
cell_0 = table.cell(i,col_index[0]).value
cell_1 = table.cell(i,col_index[1]).value
cell_2 = table.cell(i,col_index[2]).value
if (cell_0>=1 and cell_1>=1 and cell_2>=1) or (cell_0<=-1 and cell_1<=-1 and cell_2<=-1):
result.append(table.row_values(i)) # 将符合要求的一行添加进去
count += 1
print u'该文件中共%s行,%s列,其中满足条件的共有%s条数据' %(nrows, ncols, count)
print u'正在写入数据……'
col_name = col_format[0]+col_format[1]+col_format[2]
if write_data(result, col_name):
print u'写入成功!'
except Exception, e:
print u'你操作错误:%s' %e
return None
else:
print u'操作失败'
return None
def write_data(data, name):
"""写入数据,data为符合条件的数据列表,name表示指定的哪三个列,以此命名"""
file = xlwt.Workbook()
table = file.add_sheet(name,cell_overwrite_ok=True)
l = 0 # 表示行
for line in data:
c = 0 # 表示一行下的列数
for col in line:
table.write(l,c,line[c])
c += 1
l += 1
defatul_f = r'C:\Users\Administrator\Desktop\pytool\xlrd' # 默认路径
f = raw_input(u'请选择保存文件的路径:按回车跳过:')
f_name = r'\%s.xls' % name
filepath = [defatul_f+f_name, f+f_name][f != '']
file.save(filepath)
return True
def main():
handle_data()
if __name__ == '__main__':
main()
H. python可以处理excel数据吗
python处理excel数据的方法:1、使用xlrd来处理;2、使用【xlutils+xlrd】来处理;3、使用xlwt来处理;4、使用pyExcelerator来处理;5、使用Pandas库来处理。
I. python读取表格,如何循环输出一列一列内容,而不是输出全部列内容
表格也不给,sheet.col_values(2)就是直接获取第2列所有内容了(从第0列开始算)
你想要的输出格式是什么
J. python如何循环读取excel里面每个单元格的内容,我要将每一个单元格的内容同其他单元格进行比较
先安装xlrd模块
#-*-coding:utf8-*-
importxlrd
fname="reflect.xls"
bk=xlrd.open_workbook(fname)
shxrange=range(bk.nsheets)
try:
sh=bk.sheet_by_name("Sheet1")
except:
print"nosheetin%snamedSheet1"%fname
#获取行数
nrows=sh.nrows
#获取列数
ncols=sh.ncols
print"nrows%d,ncols%d"%(nrows,ncols)
#获取第一行第一列数据
cell_value=sh.cell_value(1,1)
#printcell_value
row_list=[]
#获取各行数据
foriinrange(1,nrows):
row_data=sh.row_values(i)
row_list.append(row_data)