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

Python基础之excel

时间:2017-07-19 00:13:06      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:sheet   for   iter   ice   取数据   orm   写入   write   .com   

python读取excel主要是xlrd,python写入和创建excel文件可以用xlwt、XlsxWriter,xlwt支持office2013,XlsxWriter支持office2013及以上文件。

创建和读取excel代码testXlrd.py:

#coding=utf-8

import xlrd
import xlwt

#通过xlrd读取数据
def readExcel():
    filePath="d:\\a.xlsx"
    data = xlrd.open_workbook(filePath)
    table0 = data.sheets()[0]
    nrows = table0.nrows
    for i in range(nrows):
        if i == 0: # 跳过第一行
            continue
        print(table0.row_values(i)[:5]) # 取前5列

#通过xlwt写入数据
def writeExcel():
    workbook = xlwt.Workbook() #注意Workbook的开头W要大写
    sheet1 = workbook.add_sheet(‘sheet1‘)
    #向sheet页中写入数据
    sheet1.write(0,0,‘用户名‘)
    sheet1.write(0,1,‘邮箱‘)
    sheet1.write(1,0,‘tom‘)
    sheet1.write(1,1,‘tom@qq.com‘)
    workbook.save(‘d:\\b.xls‘)
    print(‘创建excel文件完成!‘)

调用代码:

Python 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 17:54:52) [MSC v.1900 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> from testXlrd import *
>>> writeExcel()
创建excel文件完成!
>>> readExcel()
[1.0, ‘joyet1‘]
[2.0, ‘joyet2‘]
[3.0, ‘joyet3‘]
[4.0, ‘joyet4‘]
[5.0, ‘joyet5‘]
[6.0, ‘joyet6‘]
[7.0, ‘joyet7‘]
[8.0, ‘joyet8‘]
[9.0, ‘joyet9‘]
>>> 

 

Python基础之excel

标签:sheet   for   iter   ice   取数据   orm   写入   write   .com   

原文地址:http://www.cnblogs.com/joyet-john/p/7203364.html

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