python3学习(十一)——excel读、写、修改
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python3学习(十一)——excel读、写、修改,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1647字,纯文字阅读大概需要3分钟。
内容图文
![python3学习(十一)——excel读、写、修改](/upload/InfoBanner/zyjiaocheng/853/3b2907cc5d714d6f8e4817c579406430.jpg)
1、读excel
import xlrd book = xlrd.open_workbook('金牛座.xls') sheet = book.sheet_by_index(0) #sheet = book.sheet_by_name('sheet1') print(sheet.nrows) #excel里面有多少行 print(sheet.ncols) #excel中有多少列 print(sheet.cell(0,0).value)#获取指定单元格的内容 print(sheet.cell(0,1).value) #获取整行整列的内容,将获取到的内容存到list里 print(sheet.row_values(1)) print(sheet.col_values(1)) for i in range(sheet.nrows):#循环获取每行的内容 print(sheet.row_values(i))
2、写excel
import xlwt #只能写excel import xlrd #只能读excel import xlutils #修改excel!重要! #写excel book = xlwt.Workbook() sheet = book.add_sheet('sheet1') sheet.write(0,0,'id') #指定行和列写内容 sheet.write(0,1,'username') sheet.write(0,2,'password') sheet.write(1,0,'1') sheet.write(1,1,'linhuizhen') sheet.write(1,2,'123456') #################################### stus = [ [1,'njf','1234'], [2,'xiaojun','1234'], [3,'hailong','1234'], [4,'xiaohei','1234'], [5,'xiaohei','1234'], [6,'xiaohei','1234'], [7,'xiaohei','1234'], [8,'xiaohei','1234'], [9,'xiaohei','1234'], ] line = 0#控制的是行 for stu in stus: #外面的循环控制 行 #stu = [1,'njf','1234'] col = 0 # 控制列 for s in stu: #内部循环控制 列 #0行 0列 1 #0行 1列 njf #0行 2列 1234 sheet.write(line,col,s) col += 1 line += 1 book.save('stu.xls')#只能用.xls结尾 ''' #双重循环,循环了5*10=50次 for i in range(5): for j in range(10): print('haha') '''
3、修改excel
#修改excel很重要!与xlrd配合用 import xlutils import xlrd from xlutils import copy #从xlutils中导入copy这个功能 book = xlrd.open_workbook('stu.xls') #先用xlrd打开一个excel new_book = copy.copy(book) #然后用xlutils里面的copy功能,复制一个excel sheet = new_book.get_sheet(0)#获取sheet页 sheet.write(0,1,'test') sheet.write(1,1,'test2') new_book.save('stu.xls')
内容总结
以上是互联网集市为您收集整理的python3学习(十一)——excel读、写、修改全部内容,希望文章能够帮你解决python3学习(十一)——excel读、写、修改所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。