Python將多份excel表格整理成一份表格
更新時間: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("輸入需要讀取起始行號\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ù)字例如身份證號碼,設(shè)置為文本格式即可\n")
w.save('result.xls')
os.system("pause")
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- Python批量合并有合并單元格的Excel文件詳解
- Python將多個excel表格合并為一個表格
- Python將多個excel文件合并為一個文件
- python之DataFrame實(shí)現(xiàn)excel合并單元格
- Python合并多個Excel數(shù)據(jù)的方法
- python 實(shí)現(xiàn)讀取一個excel多個sheet表并合并的方法
- python合并同類型excel表格的方法
- 使用Python橫向合并excel文件的實(shí)例
- python實(shí)現(xiàn)數(shù)據(jù)寫入excel表格
- Python實(shí)現(xiàn)合并excel表格的方法分析
相關(guān)文章
pytorch常用函數(shù)定義及resnet模型修改實(shí)例
這篇文章主要為大家介紹了pytorch常用函數(shù)定義及resnet模型修改實(shí)例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06
Python實(shí)現(xiàn)針對json中某個關(guān)鍵字段進(jìn)行排序操作示例
這篇文章主要介紹了Python實(shí)現(xiàn)針對json中某個關(guān)鍵字段進(jìn)行排序操作,涉及Python json數(shù)組排序及l(fā)ambda表達(dá)式相關(guān)操作技巧,需要的朋友可以參考下2018-12-12
pytorch 實(shí)現(xiàn)在測試的時候啟用dropout
這篇文章主要介紹了pytorch 實(shí)現(xiàn)在測試的時候啟用dropout的操作,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-05-05
driver = webdriver.Chrome()報(bào)錯問題及解決
這篇文章主要介紹了driver = webdriver.Chrome()報(bào)錯問題及解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-02-02
利用Anaconda完美解決Python 2與python 3的共存問題
Anaconda 是 Python 的一個發(fā)行版,如果把 Python 比作 Linux,那么 Anancoda 就是 CentOS 或者 Ubuntu,下面這篇文章主要給大家介紹了利用Anaconda完美解決Python 2與python 3共存問題的相關(guān)資料,文中介紹的非常詳細(xì),需要的朋友可以參考借鑒。2017-05-05

