干货来袭之Python3.4如何读写Excel?

阅读更多
我们都知道在JAVA里,我们可以使用POI来操作Excel文件,在POI的API里有HSSFWorkbook支持03版的excel的读写,有XSSFWorkbook是针对07以后的excel文件的读写,那么在python3.x(散仙使用的版本是python3.4)里,我们应该如何操作excel。

首先在python3.4里,我们可以使用新的python类库,来支持3.x之后的读写excel

针对 03版excel(xls结尾的),我们可以使用xlrd读,xlwt包来写
针对 07版excel(xlsx结尾的),我们可以使用openpyxl来操作读写excel


xlwt的下载地址https://pypi.python.org/pypi/xlwt3
xlrd的下载地址https://pypi.python.org/pypi/xlrd3
openpyx1的下载地址https://pypi.python.org/pypi/openpyxl




下载完扩展的包库之后,直接解压某个目录,然后执行命令进行安装包系统的python类库里:

python setup.py install

核心代码,如下所示:

#读取excel使用(支持03)
import xlrd
#写入excel使用(支持03)
import xlwt3
#读取execel使用(支持07)
from openpyxl import Workbook
#写入excel使用(支持07)
from openpyxl import load_workbook


def showexcel(path):
    workbook=xlrd.open_workbook(path)
    sheets=workbook.sheet_names();
    #多个sheet时,采用下面的写法打印
    #for sname in sheets:
        #print(sname)
    worksheet=workbook.sheet_by_name(sheets[0])
    #nrows=worksheet.nrows
    #nclows=worksheet.ncols
    for i in range(0,worksheet.nrows):
        row=worksheet.row(i)

        for j in range(0,worksheet.ncols):
            print(worksheet.cell_value(i,j),"\t",end="")

        print()



def writeexcel03(path):

    wb=xlwt3.Workbook()
    sheet=wb.add_sheet("xlwt3数据测试表")
    value = [["名称", "hadoop编程实战", "hbase编程实战", "lucene编程实战"], ["价格", "52.3", "45", "36"], ["出版社", "机械工业出版社", "人民邮电出版社", "华夏人民出版社"], ["中文版式", "中", "英", "英"]]
    for i in range(0,4):
        for j in range(0,len(value[i])):
            sheet.write(i,j,value[i][j])
    wb.save(path)
    print("写入数据成功!")

def writeexcel07(path):

    wb=Workbook()
    #sheet=wb.add_sheet("xlwt3数据测试表")
    sheet=wb.create_sheet(0,"xlwt3数据测试表")

    value = [["名称", "hadoop编程实战", "hbase编程实战", "lucene编程实战"], ["价格", "52.3", "45", "36"], ["出版社", "机械工业出版社", "人民邮电出版社", "华夏人民出版社"], ["中文版式", "中", "英", "英"]]
    #for i in range(0,4):
        #for j in range(0,len(value[i])):
            #sheet.write(i,j,value[i][j])

            #sheet.append(value[i])
    sheet.cell(row = 1,column= 2).value="温度"
    wb.save(path)
    print("写入数据成功!")


def read07excel(path):
    wb2=load_workbook(path)
    #print(wb2.get_sheet_names())
    ws=wb2.get_sheet_by_name("详单一")
    row=ws.get_highest_row()
    col=ws.get_highest_column()
    print("列数: ",ws.get_highest_column())
    print("行数: ",ws.get_highest_row())

    for i  in range(0,row):
        for j in range(0,col):
            print(ws.rows[i][j].value,"\t\t",end="")

        print()

    #print(ws.rows[0][0].value)
    #print(ws.rows[1][0].value)
    #print(ws.rows[0][1].value)






#excelpath=r"D://名称.xlsx"
#writepath=r"D://书籍明细07.xlsx"
#writeexcel03(writepath)
#writeexcel07(writepath)

read07path="D://名称.xlsx";


read03path=r"E:\同义词词库.xls";
#read07excel(read07path)
#read07excel(read03path)
#showexcel(excelpath);
showexcel(read03path);


最后,分享三个格式数字和日期,以及随机数生成3个小知识点



#浮点数格式化成百分比
pp="{:.1%}".format(0.2356)
print(pp)

#格式化日期
import  datetime
s=(2014,12,11,0,0,0)
d="%02d-%02d-%02d" % s[:3]
print(d)

#打印随机数
import random
#浮点数随机数
f=random.uniform(1.0,100)
#整数范围随机数
f1=random.randint(1,23)
print(f1)
print(f)


结果如下:
D:\python\python.exe D:/pythonide/pythonprojectworkspace/tnssolve/f.py
23.6%
2014-12-11
17
84.57222433948785

Process finished with exit code 0


你可能感兴趣的:(excel,python读写excel)