Python將多份excel表格整理成一份表格
更新時(shí)間:2018年01月03日 09:55:44 作者:xuyd33
這篇文章主要為大家詳細(xì)介紹了Python將多份excel表格整理成一份表格,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
利用Python將多份excel表格整理成一份表格,拋棄過去逐份打開復(fù)制粘貼的方式。
直接附上代碼:
import xlrd import xlwt import os from xlutils.copy import copy import os.path from xlwt import * dir = input("輸入文件路徑\n"); start_row = input("輸入需要讀取起始行號(hào)\n"); start_row = int(start_row) end_row = input("輸入結(jié)束行,輸入0表示有內(nèi)容的最后一行\(zhòng)n") end_row = int(end_row) #dir = 'E:\畢業(yè)資料\2013電2\\' all_file = []; def min_s(a ,b): if a == 0: return b if (a >b): return b else: return a #遍歷所有同學(xué)文件 for parent,folder,filename in os.walk(dir): for file,x in zip(filename,range(len(filename))): file = os.path.join(parent,filename[x]) print(filename[x]) all_file.append(file) print("\n文件總數(shù):",len(all_file)) if os.path.exists("result.xls"): os.remove("result.xls") w = xlwt.Workbook() row = 0; ws = w.add_sheet('sheet1',cell_overwrite_ok=True) style = XFStyle() fnt = Font() fnt.height = 240 fnt.name = u'宋體' style.font = fnt align = Alignment() align.horz = 2 style.alignment = align for single_file_path in all_file: data = xlrd.open_workbook(single_file_path); sheet = data.sheet_by_index(0) if sheet.nrows >= start_row: for i in range(start_row-1,min_s(end_row,sheet.nrows)): list = sheet.row_values(i) for col in range(0,len(list)): ws.write(row,col,list[col],style) row = row + 1; else: print("非法填寫的表格名稱:"+single_file_path) #寫入目標(biāo)文件 print("運(yùn)行結(jié)束,結(jié)果保存在result.xls文件里\n") print("對于日期,可將對應(yīng)單元格設(shè)置為為日期格式便可正確顯示\n" "對于超長數(shù)字例如身份證號(hào)碼,設(shè)置為文本格式即可\n") w.save('result.xls') os.system("pause")
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- Python批量合并有合并單元格的Excel文件詳解
- Python將多個(gè)excel表格合并為一個(gè)表格
- Python將多個(gè)excel文件合并為一個(gè)文件
- python之DataFrame實(shí)現(xiàn)excel合并單元格
- Python合并多個(gè)Excel數(shù)據(jù)的方法
- python 實(shí)現(xiàn)讀取一個(gè)excel多個(gè)sheet表并合并的方法
- python合并同類型excel表格的方法
- 使用Python橫向合并excel文件的實(shí)例
- python實(shí)現(xiàn)數(shù)據(jù)寫入excel表格
- Python實(shí)現(xiàn)合并excel表格的方法分析
相關(guān)文章
Python中使用glob和rmtree刪除目錄子目錄及所有文件的例子
這篇文章主要介紹了python中使用glob和rmtree刪除目錄子目錄及所有文件的例子,需要的朋友可以參考下2014-11-11用TensorFlow實(shí)現(xiàn)lasso回歸和嶺回歸算法的示例
本篇文章主要介紹了用TensorFlow實(shí)現(xiàn)lasso回歸和嶺回歸算法的示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-05-05