当前位置: 移动技术网 > IT编程>脚本编程>Python > python读取excel指定列数据并写入到新的excel方法

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

2018年08月06日  | 移动技术网IT编程  | 我要评论

冷酷少主霸宠小逃妻,电磁炉烧功率管,pppd-287

如下所示:

#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读取excel指定列数据并写入到新的excel方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网