安裝兩個庫:pip install xlrd、pip install xlwt
1.python讀excel——xlrd
2.python寫excel——xlwt
1.讀excel數(shù)據(jù),包括日期等數(shù)據(jù)
#coding=utf-8
import xlrd
import datetime
from datetime import date
def read_excel():
#打開文件
wb = xlrd.open_workbook(r'test.xlsx')
#獲取所有sheet的名字
print(wb.sheet_names())
#獲取第二個sheet的表明
sheet2 = wb.sheet_names()[1]
#sheet1索引從0開始,得到sheet1表的句柄
sheet1 = wb.sheet_by_index(0)
rowNum = sheet1.nrows
colNum = sheet1.ncols
#s = sheet1.cell(1,0).value.encode('utf-8')
s = sheet1.cell(1,0).value
#獲取某一個位置的數(shù)據(jù)
# 1 ctype : 0 empty,1 string, 2 number, 3 date, 4 boolean, 5 error
print(sheet1.cell(1,2).ctype)
print(s)
#print(s.decode('utf-8'))
#獲取整行和整列的數(shù)據(jù)
#第二行數(shù)據(jù)
row2 = sheet1.row_values(1)
#第二列數(shù)據(jù)
cols2 = sheet1.col_values(2)
#python讀取excel中單元格內(nèi)容為日期的方式
#返回類型有5種
for i in range(rowNum):
if sheet1.cell(i,2).ctype == 3:
d = xlrd.xldate_as_tuple(sheet1.cell_value(i,2),wb.datemode)
print(date(*d[:3]),end='')
print('\n')
if __name__ == '__main__':
read_excel()~
運行效果
2.往excel寫入數(shù)據(jù)
#coding=utf-8
import xlwt
#設(shè)置表格樣式
def set_stlye(name,height,bold=False):
#初始化樣式
style = xlwt.XFStyle()
#創(chuàng)建字體
font = xlwt.Font()
font.bold = bold
font.colour_index = 4
font.height = height
font.name =name
style.font = font
return style
#寫入數(shù)據(jù)
def write_excel():
f = xlwt.Workbook()
#創(chuàng)建sheet1鄭州人流醫(yī)院 http://mobile.zzzzyy120.com/
sheet1 = f.add_sheet(u'sheet1',cell_overwrite_ok=True)
row0 = [u'業(yè)務(wù)',u'狀態(tài)',u'北京',u'上海',u'廣州',u'深圳',u'狀態(tài)小計',u'合計']
column0 = [u'機票',u'船票',u'火車票',u'汽車票',u'其他']
status = [u'預(yù)定',u'出票',u'退票',u'業(yè)務(wù)小計']
for i in range(0,len(row0)):
sheet1.write(0,i,row0[i],set_stlye("Time New Roman",220,True))
i,j = 1,0
while i <4*len(column0): #控制循環(huán):每次加4
#第一列
sheet1.write_merge(i,i+3,0,0,column0[j],set_stlye('Arial',220,True))
#最后一列
sheet1.write_merge(i,i+3,7,7)
i += 4
sheet1.write_merge(21,21,0,1,u'合計',set_stlye("Time New Roman",220,True))
i=0
while i<4*len(column0): #控制外層循環(huán):每次加4
for j in range(0,len(status)): #控制內(nèi)層循環(huán):設(shè)置每一行內(nèi)容
sheet1.write(i+j+1,1,status[j])
i += 4
#創(chuàng)建sheet2
sheet2 = f.add_sheet(u'sheet2',cell_overwrite_ok=True)
row0 = [u'姓名',u'年齡',u'出生日期',u'愛好',u'關(guān)系']
column0 = [u'UZI',u'Faker',u'大司馬',u'PDD',u'馮提莫']
#生成第一行
for i in range(0,len(row0)):
sheet2.write(0,i,row0[i],set_stlye('Times New Roman',220,True))
#生成第一列
for i in range(0,len(column0)):
sheet2.write(i+1,0,column0[i],set_stlye('Times New Roman',220,True))
f.save('data.xls')
if __name__ == '__main__':
write_excel()~
在data.xls種生成了sheet1和sheet2
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
網(wǎng)站欄目:python讀寫Excel表格的簡單方法-創(chuàng)新互聯(lián)
文章轉(zhuǎn)載:http://jinyejixie.com/article22/dpcecc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供用戶體驗、網(wǎng)站建設(shè)、App開發(fā)、品牌網(wǎng)站設(shè)計、響應(yīng)式網(wǎng)站、網(wǎng)站改版
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容