日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

python中 xlrd/xlwt模块详解

發(fā)布時(shí)間:2023/12/14 python 45 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python中 xlrd/xlwt模块详解 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

python中 xlrd/xlwt模塊詳解?

?

1、什么是xlrd模塊

python操作excel主要用到xlrd和xlwt兩個(gè)庫,即xlrd是讀excel,xlwt是寫excel庫

一、安裝xlrd模塊

cmd窗口下 pip install xlrd

二、使用介紹

# -*- coding=utf-8 -*-import xlrd #導(dǎo)入讀excel的模塊#打開excel filename='C:/Users/YKDZ065/PycharmProjects/0830/jianlc/cases/Test.xlsx' data=xlrd.open_workbook(filename,"rb")#獲取excel工作表 mysheets=data.sheets() #獲取工作表list#通過索引獲取第一個(gè)sheet mysheet=mysheets[0]#通過索引順序獲取 #mysheet=data.sheet_by_index(0)#通過名稱獲取 #mysheet=data.sheet_by_name(u'Sheet1')#獲取行數(shù)和列數(shù) nrows=mysheet.nrows print nrows ncols=mysheet.ncols print ncols#獲取一行和一列 #myRowValues=mysheet.row_values(0) #print myRowValues #myColValues=mysheet.col_values(0) #print myColValues#讀取單元格數(shù)據(jù) for i in range(ncols):for j in range(nrows):myCell=mysheet.cell(j,i)myCellValue=myCell.valueprint myCellValue

2、使用xlwt模塊寫入excel文件

# -*- coding=utf-8 -*- #導(dǎo)入模塊 import xlwt#創(chuàng)建excel工作薄 myWorkbook=xlwt.Workbook()#添加Excel工作表 mySheet=myWorkbook.add_sheet("a Test Sheet")#寫入數(shù)據(jù) myStyle=xlwt.easyxf('font: name Times New Roman, color-index red, bold on', num_format_str='#,##0.00') #數(shù)據(jù)格式 mySheet.write(1,1,1234.56,myStyle) mySheet.write(2,0,1) mySheet.write(2,1,1) mySheet.write(2,2,xlwt.Formula("A3+B3"))myWorkbook.save("excelFile.xlsx")

?

3、xlutils結(jié)合xlrd可以達(dá)到修改excel文件目的

# -*- coding=utf-8 -*- #修改excel文件 #導(dǎo)入包 import xlrd from xlutils.copy import copy filename='C:/Users/YKDZ065/PycharmProjects/0830/jianlc/cases/Test.xlsx' workbook=xlrd.open_workbook(filename) workbooknew=copy(workbook) ws=workbooknew.get_sheet(0) ws.write(0,1,"changed") workbooknew.save("Testcopy.xlsx")

?

# -*- coding=utf-8 -*- # author=zyq import xlrd #讀excel的模塊 import xlwt #寫excel的模塊 #操作excel文件 def opExcel():filename='excelFile.xlsx' #定義一個(gè)excel文件 excelFile=xlrd.open_workbook(filename) #打開excel文件 table=excelFile.sheets()[0] #通過索引獲取第一個(gè)sheet表 #table=excelFile.sheet_by_index(0) #通過索引獲取第一個(gè)sheet表 #table=excelFile.sheet_by_name(u"a Test Sheet")# 通過名字獲取sheet表 #獲取整行的值 print table.row_values(0) #獲取第一行的值,以列表的形式返回 print table.col_values(0) #獲取第一列的值,以列表的形式返回 #獲取行數(shù)和列表 print table.nrows #獲取行數(shù) print table.ncols #獲取列表 #根據(jù)行數(shù)遍歷所有行數(shù)的值 for i in range(table.nrows):print table.row_values(i)#獲取單元格的值 cell_A1=table.cell(0,0).value #獲取A1位置的數(shù)據(jù),行和列的所有位置都是從0開始的 print cell_A1#使用行列所有確定單元格的數(shù)據(jù) cell_A1=table.row(0)[0].valueprint cell_A1#獲取所有單元格的值 for i in range(table.nrows):for j in range(table.ncols):print table.cell(i,j).valueopExcel() # 對(duì)excel的寫操作 def writeExcel():#創(chuàng)建excel mybook=xlwt.Workbook()#添加excel工作表 mySheet=mybook.add_sheet("a")#寫入數(shù)據(jù) mySheet.write(4,4,'test')mybook.save("1.xlsx") writeExcel()

# -*- coding: utf-8 -*-
import? xdrlib ,sys
import xlrd
def open_excel(file= 'file.xls'):
??? try:
??????? data = xlrd.open_workbook(file)
??????? return data
??? except Exception,e:
??????? print str(e)
#根據(jù)索引獲取Excel表格中的數(shù)據(jù)?? 參數(shù):file:Excel文件路徑???? colnameindex:表頭列名所在行的所以? ,by_index:表的索引
def excel_table_byindex(file= 'file.xls',colnameindex=0,by_index=0):
??? data = open_excel(file)
??? table = data.sheets()[by_index]
??? nrows = table.nrows #行數(shù)
??? ncols = table.ncols #列數(shù)
??? colnames =? table.row_values(colnameindex) #某一行數(shù)據(jù)
??? list =[]
??? for rownum in range(1,nrows):

???????? row = table.row_values(rownum)
???????? if row:
???????????? app = {}
???????????? for i in range(len(colnames)):
??????????????? app[colnames[i]] = row[i]
???????????? list.append(app)
??? return list

#根據(jù)名稱獲取Excel表格中的數(shù)據(jù)?? 參數(shù):file:Excel文件路徑???? colnameindex:表頭列名所在行的所以? ,by_name:Sheet1名稱
def excel_table_byname(file= 'file.xls',colnameindex=0,by_name=u'Sheet1'):
??? data = open_excel(file)
??? table = data.sheet_by_name(by_name)
??? nrows = table.nrows #行數(shù)
??? colnames =? table.row_values(colnameindex) #某一行數(shù)據(jù)
??? list =[]
??? for rownum in range(1,nrows):
???????? row = table.row_values(rownum)
???????? if row:
???????????? app = {}
???????????? for i in range(len(colnames)):
??????????????? app[colnames[i]] = row[i]
???????????? list.append(app)
??? return list

def main():
?? tables = excel_table_byindex()
?? for row in tables:
?????? print row

?? tables = excel_table_byname()
?? for row in tables:
?????? print row

if __name__=="__main__":
??? main()

總結(jié)

以上是生活随笔為你收集整理的python中 xlrd/xlwt模块详解的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。