Python實(shí)現(xiàn)自定義順序、排列寫入數(shù)據(jù)到Excel的方法
本文實(shí)例講述了Python實(shí)現(xiàn)自定義順序、排列寫入數(shù)據(jù)到Excel的方法。分享給大家供大家參考,具體如下:
例1. 數(shù)據(jù)框順序?qū)懭隕xcel:
data=a import xlsxwriter workbook = xlsxwriter.Workbook('F:/chart1.xlsx') worksheet = workbook.add_worksheet('請(qǐng)求接口') title = [u'訂單號(hào)',u'債權(quán)編號(hào)',u'請(qǐng)求參數(shù)',u'創(chuàng)建時(shí)間',u'結(jié)果'] print data.iloc[:,0] format=workbook.add_format() format.set_border(1) format_title=workbook.add_format() format_title.set_border(1) format_title.set_bg_color('#cccccc') format_title.set_align('center') format_title.set_bold() format_ave=workbook.add_format() format_ave.set_border(1) format_ave.set_num_format('0.00') worksheet.write_row('A1',title,format_title) worksheet.write_column('A2:', data.iloc[:,0],format) worksheet.write_column('B2', data.iloc[:,1],format) worksheet.write_column('C2', data.iloc[:,2],format) worksheet.write_column('D2', data.iloc[:,3],format) worksheet.write_column('E2', data.iloc[:,4],format) workbook.close()
例2. (自動(dòng)報(bào)表):
#coding: utf-8 import xlsxwriter workbook = xlsxwriter.Workbook('F:/chart.xlsx') worksheet = workbook.add_worksheet() chart = workbook.add_chart({'type': 'column'}) title = [u'業(yè)務(wù)名稱',u'星期一',u'星期二',u'星期三',u'星期四',u'星期五',u'星期六',u'星期日',u'平均流量'] buname= [u'業(yè)務(wù)官網(wǎng)',u'新聞中心',u'購物頻道',u'體育頻道',u'親子頻道'] data = [ [150,152,158,149,155,145,148], [89,88,95,93,98,100,99], [201,200,198,175,170,198,195], [75,77,78,78,74,70,79], [88,85,87,90,93,88,84], ] print data format=workbook.add_format() format.set_border(1) format_title=workbook.add_format() format_title.set_border(1) format_title.set_bg_color('#cccccc') format_title.set_align('center') format_title.set_bold() format_ave=workbook.add_format() format_ave.set_border(1) format_ave.set_num_format('0.00') worksheet.write_row('A1',title,format_title) worksheet.write_column('A2', buname,format) worksheet.write_row('B2', data[0],format) worksheet.write_row('B3', data[1],format) worksheet.write_row('B4', data[2],format) worksheet.write_row('B5', data[3],format) worksheet.write_row('B6', data[4],format) def chart_series(cur_row): worksheet.write_formula('I'+cur_row, \ '=AVERAGE(B'+cur_row+':H'+cur_row+')',format_ave) chart.add_series({ 'categories': '=Sheet1!$B$1:$H$1', 'values': '=Sheet1!$B$'+cur_row+':$H$'+cur_row, 'line': {'color': 'black'}, 'name': '=Sheet1!$A$'+cur_row, }) for row in range(2, 7): chart_series(str(row)) chart.set_table() chart.set_style(30) chart.set_size({'width': 577, 'height': 287}) chart.set_title ({'name': u'業(yè)務(wù)流量周報(bào)圖表'}) chart.set_y_axis({'name': 'Mb/s'}) worksheet.insert_chart('A8', chart) workbook.close()
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python操作Excel表格技巧總結(jié)》、《Python文件與目錄操作技巧匯總》、《Python文本文件操作技巧匯總》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》及《Python入門與進(jìn)階經(jīng)典教程》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
淺談keras通過model.fit_generator訓(xùn)練模型(節(jié)省內(nèi)存)
這篇文章主要介紹了淺談keras通過model.fit_generator訓(xùn)練模型(節(jié)省內(nèi)存),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-06-06python中的break、continue、exit()、pass全面解析
下面小編就為大家?guī)硪黄猵ython中的break、continue、exit()、pass全面解析。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-08-08Python實(shí)現(xiàn)快速多線程ping的方法
這篇文章主要介紹了Python實(shí)現(xiàn)快速多線程ping的方法,實(shí)例分析了Python多線程及ICMP數(shù)據(jù)包的發(fā)送技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07Python深入了解defaultdict之輕松處理默認(rèn)值與復(fù)雜數(shù)據(jù)結(jié)構(gòu)
在Python標(biāo)準(zhǔn)庫collections模塊中,defaultdict提供了一種在字典訪問不存在的鍵時(shí)自動(dòng)提供默認(rèn)值的便利方式,這篇文章詳細(xì)介紹了defaultdict的使用方法、基礎(chǔ)概念、創(chuàng)建實(shí)例的步驟以及應(yīng)用場(chǎng)景,需要的朋友可以參考下2024-09-09對(duì)python實(shí)現(xiàn)模板生成腳本的方法詳解
今天小編就為大家分享一篇對(duì)python實(shí)現(xiàn)模板生成腳本的方法詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-01-01Python中各類Excel表格批量合并問題的實(shí)現(xiàn)思路與案例
在日常工作中,可能會(huì)遇到各類表格合并的需求。本文主要介紹了Python中各類Excel表格批量合并問題的實(shí)現(xiàn)思路與案例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01Python3+OpenCV2實(shí)現(xiàn)圖像的幾何變換(平移、鏡像、縮放、旋轉(zhuǎn)、仿射)
這篇文章主要介紹了Python3+OpenCV2實(shí)現(xiàn)圖像的幾何變換(平移、鏡像、縮放、旋轉(zhuǎn)、仿射),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-05-05