欢迎来到代码驿站!

Python代码

当前位置:首页 > 软件编程 > Python代码

python读取excel指定列数据并写入到新的excel方法

时间:2021-02-15 10:22:55|栏目:Python代码|点击:

如下所示:

#encoding=utf-8
import xlrd
from xlwt import *
#------------------读数据---------------------------------
fileName="C:\\Users\\st\\Desktop\\test\\20170221131701.xlsx"
bk=xlrd.open_workbook(fileName)
shxrange=range(bk.nsheets)
try:
  sh=bk.sheet_by_name("Sheet1")
except:
  print "代码出错"
nrows=sh.nrows #获取行数
book = Workbook(encoding='utf-8')
sheet = book.add_sheet('Sheet1') #创建一个sheet
for i in range(1,nrows):
  row_data=sh.row_values(i)
  #获取第i行第3列数据
  #sh.cell_value(i,3)
  #---------写出文件到excel--------
  print "-----正在写入 "+str(i)+" 行"
  sheet.write(i,1, label = sh.cell_value(i,3)) #向第1行第1列写入获取到的值
  sheet.write(i,2, label = sh.cell_value(i,5)) #向第1行第2列写入获取到的值
book.save("C:\\Users\\st\\Desktop\\test\\demo1.xls")

上一篇:python将html转成PDF的实现代码(包含中文)

栏    目:Python代码

下一篇:centos 安装Python3 及对应的pip教程详解

本文标题:python读取excel指定列数据并写入到新的excel方法

本文地址:http://www.codeinn.net/misctech/63532.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有