python中 xlrd/xlwt模块详解
python中 xlrd/xlwt模塊詳解?
?
1、什么是xlrd模塊
python操作excel主要用到xlrd和xlwt兩個庫,即xlrd是讀excel,xlwt是寫excel庫
一、安裝xlrd模塊
cmd窗口下 pip install xlrd
二、使用介紹
# -*- coding=utf-8 -*-import xlrd #導入讀excel的模塊#打開excel filename='C:/Users/YKDZ065/PycharmProjects/0830/jianlc/cases/Test.xlsx' data=xlrd.open_workbook(filename,"rb")#獲取excel工作表 mysheets=data.sheets() #獲取工作表list#通過索引獲取第一個sheet mysheet=mysheets[0]#通過索引順序獲取 #mysheet=data.sheet_by_index(0)#通過名稱獲取 #mysheet=data.sheet_by_name(u'Sheet1')#獲取行數和列數 nrows=mysheet.nrows print nrows ncols=mysheet.ncols print ncols#獲取一行和一列 #myRowValues=mysheet.row_values(0) #print myRowValues #myColValues=mysheet.col_values(0) #print myColValues#讀取單元格數據 for i in range(ncols):for j in range(nrows):myCell=mysheet.cell(j,i)myCellValue=myCell.valueprint myCellValue2、使用xlwt模塊寫入excel文件
# -*- coding=utf-8 -*- #導入模塊 import xlwt#創建excel工作薄 myWorkbook=xlwt.Workbook()#添加Excel工作表 mySheet=myWorkbook.add_sheet("a Test Sheet")#寫入數據 myStyle=xlwt.easyxf('font: name Times New Roman, color-index red, bold on', num_format_str='#,##0.00') #數據格式 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結合xlrd可以達到修改excel文件目的
# -*- coding=utf-8 -*- #修改excel文件 #導入包 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' #定義一個excel文件 excelFile=xlrd.open_workbook(filename) #打開excel文件 table=excelFile.sheets()[0] #通過索引獲取第一個sheet表 #table=excelFile.sheet_by_index(0) #通過索引獲取第一個sheet表 #table=excelFile.sheet_by_name(u"a Test Sheet")# 通過名字獲取sheet表 #獲取整行的值 print table.row_values(0) #獲取第一行的值,以列表的形式返回 print table.col_values(0) #獲取第一列的值,以列表的形式返回 #獲取行數和列表 print table.nrows #獲取行數 print table.ncols #獲取列表 #根據行數遍歷所有行數的值 for i in range(table.nrows):print table.row_values(i)#獲取單元格的值 cell_A1=table.cell(0,0).value #獲取A1位置的數據,行和列的所有位置都是從0開始的 print cell_A1#使用行列所有確定單元格的數據 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() # 對excel的寫操作 def writeExcel():#創建excel mybook=xlwt.Workbook()#添加excel工作表 mySheet=mybook.add_sheet("a")#寫入數據 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)
#根據索引獲取Excel表格中的數據?? 參數: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 #行數
??? ncols = table.ncols #列數
??? colnames =? table.row_values(colnameindex) #某一行數據
??? 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
#根據名稱獲取Excel表格中的數據?? 參數: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 #行數
??? colnames =? table.row_values(colnameindex) #某一行數據
??? 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()
總結
以上是生活随笔為你收集整理的python中 xlrd/xlwt模块详解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 超级玛丽
- 下一篇: java中滚动字幕做法_四种滚动字幕的方