小編給大家分享一下如何用python將excel數(shù)據(jù)合并,希望大家閱讀完這篇文章后大所收獲,下面讓我們一起去探討吧!
創(chuàng)新互聯(lián)主要從事網(wǎng)站建設(shè)、做網(wǎng)站、網(wǎng)頁設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)平鄉(xiāng),10余年網(wǎng)站建設(shè)經(jīng)驗(yàn),價(jià)格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):18980820575
xlsxwriter使用介紹:
import xlsxwriter # 創(chuàng)建一個(gè)工作簿并添加一個(gè)工作表 workbook = xlsxwriter.Workbook("demo.xlsx") worksheet = workbook.add_worksheet() # 設(shè)置列寬 worksheet.set_column("A:A", 20) # 設(shè)置格式 bold = workbook.add_format({"bold": True}) # 設(shè)置單元格的值 worksheet.write("A1", "Hello") # 帶格式的單元格 worksheet.write("A2", "World") # 寫一些數(shù)字,用行列標(biāo)識(shí) worksheet.write(2, 0, 123) worksheet.write(3, 0, 123.456, bold) # 插入一張圖片 worksheet.insert_image("B5", "C:/Users/Cheng/Desktop/1.png") # 關(guān)閉文件流 workbook.close()
合并Excel數(shù)據(jù)的方法:
import xlrd import xlsxwriter source_xls = ["D:/python/1.xlsx", "D:/python/2.xlsx"] target_xls = "D:/python/3.xlsx" # 讀取數(shù)據(jù) data = [] for i in source_xls: wb = xlrd.open_workbook(i) for sheet in wb.sheets(): for rownum in range(sheet.nrows): data.append(sheet.row_values(rownum)) print(data) # 寫入數(shù)據(jù) workbook = xlsxwriter.Workbook(target_xls) worksheet = workbook.add_worksheet() font = workbook.add_format({"font_size":14}) for i in range(len(data)): for j in range(len(data[i])): worksheet.write(i, j, data[i][j], font) # 關(guān)閉文件流 workbook.close()
將所有文件中的所有sheet中的數(shù)據(jù)讀取出來組成一個(gè)二維數(shù)組,然后再寫入新的Excel。
看完了這篇文章,相信你對如何用python將excel數(shù)據(jù)合并有了一定的了解,想了解更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!