欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python3中關(guān)于excel追加寫入格式被覆蓋問題(實(shí)例代碼)

 更新時(shí)間:2020年01月10日 09:03:22   作者:鵬神哥哥  
這篇文章主要介紹了python3中關(guān)于excel追加寫入格式被覆蓋問題,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

關(guān)于python3中的追加寫入excel問題,這個(gè)問題坑了我?guī)仔r(shí),其實(shí)加一個(gè)參數(shù)即可。

因?yàn)橹坝袑懞玫膃xcel,想追加寫入,但是寫入后卻只有寫入后的單元格格式,之前寫的完全消失。

以下是我的代碼

這代碼可以用是我做的一個(gè)爬蟲維護(hù)項(xiàng)目:

def times():
  User_Agent = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36'
  headers = {
    'User-Agent': User_Agent
  }
  search_url = 'https://www.cnss.com.cn/u/cms/www/indexJson/bdi_month.json?v=1577414941357'
  request = urllib.request.Request(search_url, headers=headers)
  response = urllib.request.urlopen(request)
  content = response.read().decode('utf-8')
  content = ''.join(content)
  # print(content)
  # index = re.findall('index":"(.*?)"', content) # 獲取指數(shù)
  date = re.findall('date":"(.*?)"', content) # 獲取時(shí)間
  time = []
  i = 0
  start_date = date[0].replace('.', '年')
  start_date.replace('.', '月')
  end_date = date[-1].replace('.', '年')
  end_date.replace('.', '月')
  # print(index,date,start_date,end_date)
  for j in range(int(len(date) / 1)):
    temp = date[i:i + 1]
    i += 1
    time.append(temp)
    hears = start_date + '日' + '——' + end_date + '日' + '嘻嘻嘻'
    title=['交易日期','干散貨指數(shù)(BDI)','海岬型指數(shù)(BCI)','巴拿馬型指數(shù)(BPI)','超靈便型船運(yùn)價(jià)指數(shù)(BSI)','靈便型船指數(shù)(BHSI)']
    sheet1.write_merge(0, 0+0,0,0+5, hears, style)
    for ti in range(len(title)):
      sheet1.write(1, ti+0, title[ti], style)
    for x in range(len(time)):
      for y in range(len(time[x])):
        sheet1.write(x + 2, 0, time[x][y], style)
    f.save('你想放的路徑.xls')

上面的代碼還是可以繼續(xù)使用

標(biāo)題xlwt的缺陷:

xlwt只能創(chuàng)建一個(gè)全新的excel文件,然后對(duì)這個(gè)文件進(jìn)行寫入內(nèi)容以及保存。但是大多數(shù)情況下我們希望的是讀入一個(gè)excel文件,然后進(jìn)行修改或追加,這個(gè)時(shí)候就需要xlutils了。

xlutils的簡單使用:

接下來的部分就是關(guān)鍵所在了

formatting_info=True這個(gè)參數(shù)能保留原excel格式

def write_excel_xls_append(path, value,u):
  index = len(value) # 獲取需要寫入數(shù)據(jù)的行數(shù)
  workbook = xlrd.open_workbook('./result/30波羅的海干散貨運(yùn)價(jià)指數(shù).xls',formatting_info=True) # 打開工作簿
  sheets = workbook.sheet_names() # 獲取工作簿中的所有表格
  worksheet = workbook.sheet_by_name(sheets[0]) # 獲取工作簿中所有表格中的的第一個(gè)表格
  rows_old = worksheet.ncols # 獲取表格中已存在的數(shù)據(jù)的行數(shù)
  new_workbook = copy(workbook) # 將xlrd對(duì)象拷貝轉(zhuǎn)化為xlwt對(duì)象
  styleS = xlwt.XFStyle()
  alignment = xlwt.Alignment()
  alignment.horz = xlwt.Alignment.HORZ_CENTER
  alignment.vert = xlwt.Alignment.VERT_CENTER
  styleS.alignment = alignment
  new_worksheet = new_workbook.get_sheet(0) # 獲取轉(zhuǎn)化后工作簿中的第一個(gè)表格
  for i in range(0, index):
    for j in range(0, len(value[i])):
      new_worksheet.write(i+2, u+1, value[i][j],styleS) # 追加寫入數(shù)據(jù),注意是從i+rows_old行開始寫入
  new_workbook.save(path) # 保存工作簿

然后你就會(huì)發(fā)現(xiàn)你的excel簡直完美~~~

總結(jié)

以上所述是小編給大家介紹的python3中關(guān)于excel追加寫入格式被覆蓋問題,希望對(duì)大家有所幫助,如果大家有任何疑問請給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!

相關(guān)文章

最新評(píng)論