码迷,mamicode.com
首页 > 编程语言 > 详细

Python之操作Excel

时间:2018-09-28 16:27:27      阅读:115      评论:0      收藏:0      [点我收藏+]

标签:col   xiaomi   excel   模块   style   book   add   user   import   

使用之前先导入三个模块:

import xlwt  #只能写Excel
import xlrd   #只能读Excel
import xlutils  #修改Excel,在原来的基础上修改

一、写EXCEL

book=xlwt.Workbook()  #新建一个Excel
sheet=book.add_sheet(sheet1)#建一个sheet页
sheet.write(0,0,id)#指定行和列,写内容
sheet.write(0,1,username)
sheet.write(0,2,password)

sheet.write(1,0,1)
sheet.write(1,1,xiaoming)
sheet.write(1,2,123456)

book.save(stu.xls)#保存内容
#注意:要保存成结尾时.xls的文件,.xlsx用微软的文件打不开,只能用WPS的打开

使用循环方式写入内容:

#给定文件内容:
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],
]

#内容写入Excel
book=xlrt.Workbook() #新建一个Excel
sheet=book.add_sheet(sheet1) #新建一个sheet页

line=0 #控制的是行
for stu in stus:
    col=0 #控制列
    for s in stu:
        sheet.write(line,col,s)
        col+=1
    line+=1

book.save(stu.xls) #保存内容

 

Python之操作Excel

标签:col   xiaomi   excel   模块   style   book   add   user   import   

原文地址:https://www.cnblogs.com/yanwuliu/p/9718901.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!