Python将多份excel表格整理成一份表格
作者:xuyd33 时间:2022-06-11 13:54:50
利用Python将多份excel表格整理成一份表格,抛弃过去逐份打开复制粘贴的方式。
直接附上代码:
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("输入结束行,输入0表示有内容的最后一行\n")
end_row = int(end_row)
#dir = 'E:\毕业资料\2013电2\\'
all_file = [];
def min_s(a ,b):
if a == 0:
return b
if (a >b):
return b
else:
return a
#遍历所有同学文件
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文件总数:",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)
#写入目标文件
print("运行结束,结果保存在result.xls文件里\n")
print("对于日期,可将对应单元格设置为为日期格式便可正确显示\n"
"对于超长数字例如身份证号码,设置为文本格式即可\n")
w.save('result.xls')
os.system("pause")
来源:http://blog.csdn.net/xuyd33/article/details/72629522
标签:python,excel,表格
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Django学习笔记之为Model添加Action
2021-04-09 16:32:26
asp如何做一个树状展开视图来显示自己的记录结构?
2010-07-12 18:56:00
Python中列表的基本操作汇总
2021-08-20 23:21:27
SQL Server导入导出数据方法
2007-08-17 09:50:00
Flask 使用工厂模式
2021-10-01 01:52:58
![](https://img.aspxhome.com/file/2023/6/72936_0s.png)
Python实现统计单词出现的个数
2022-11-01 12:19:30
基于prototype扩展的JavaScript常用函数库
2023-08-24 15:09:57
Oracle数据库安全策略分析(一)
2010-07-31 12:57:00
python 中os模块os.path.exists()的用法说明
2022-04-30 01:22:26
基于Python函数和变量名解析
2022-10-26 13:49:56
![](https://img.aspxhome.com/file/2023/7/76727_0s.jpg)
python实现公司年会抽奖程序
2022-12-19 15:35:27
图片自适应宽度新解决方法
2009-09-22 14:55:00
Linux下安装Python3和django并配置mysql作为django默认服务器方法
2023-11-15 01:04:44
Python中requests库的用法详解
2022-04-12 07:05:16
python 实现的截屏工具
2023-05-15 00:20:39
![](https://img.aspxhome.com/file/2023/5/86215_0s.png)
jquery弹出层背景变暗 Lee dialog
2008-08-18 13:11:00
![](https://img.aspxhome.com/file/UploadPic/20088/18/2008818131847932s.gif)
GO语言原生实现文件上传功能
2023-10-17 08:29:39
![](https://img.aspxhome.com/file/2023/2/103032_0s.jpg)
python中使用多线程改进flask案例
2022-11-07 05:44:55
CSS实现垂直居中的5种方法
2009-03-04 12:53:00
![](https://img.aspxhome.com/file/UploadPic/20093/4/0613598e4-88s.jpg)
一些让Python代码简洁的实用技巧总结
2022-02-06 11:03:25