python讀寫Excel表格的簡(jiǎn)單方法-創(chuàng)新互聯(lián)

安裝兩個(gè)庫(kù):pip install xlrd、pip install xlwt

成都創(chuàng)新互聯(lián)公司主要業(yè)務(wù)有網(wǎng)站營(yíng)銷策劃、成都網(wǎng)站設(shè)計(jì)、成都做網(wǎng)站、微信公眾號(hào)開發(fā)、成都小程序開發(fā)、H5場(chǎng)景定制、程序開發(fā)等業(yè)務(wù)。一次合作終身朋友,是我們奉行的宗旨;我們不僅僅把客戶當(dāng)客戶,還把客戶視為我們的合作伙伴,在開展業(yè)務(wù)的過(guò)程中,公司還積累了豐富的行業(yè)經(jīng)驗(yàn)、全網(wǎng)整合營(yíng)銷推廣資源和合作伙伴關(guān)系資源,并逐漸建立起規(guī)范的客戶服務(wù)和保障體系。 

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())

#獲取第二個(gè)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

#獲取某一個(gè)位置的數(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()~

運(yùn)行效果

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)小計(jì)',u'合計(jì)']

column0 = [u'機(jī)票',u'船票',u'火車票',u'汽車票',u'其他']

status = [u'預(yù)定',u'出票',u'退票',u'業(yè)務(wù)小計(jì)']

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'合計(jì)',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'愛(ài)好',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元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。

網(wǎng)站欄目:python讀寫Excel表格的簡(jiǎn)單方法-創(chuàng)新互聯(lián)
文章轉(zhuǎn)載:http://muchs.cn/article22/dpcecc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供用戶體驗(yàn)、網(wǎng)站建設(shè)、App開發(fā)、品牌網(wǎng)站設(shè)計(jì)響應(yīng)式網(wǎng)站、網(wǎng)站改版

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

商城網(wǎng)站建設(shè)