溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

python讀寫Excel表格的簡(jiǎn)單方法

發(fā)布時(shí)間:2020-07-30 09:57:13 來(lái)源:網(wǎng)絡(luò) 閱讀:328 作者:nineteens 欄目:編程語(yǔ)言

  安裝兩個(gè)庫(kù):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())

  #獲取第二個(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'愛好',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


向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI