加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

使用python合并excel

发布时间:2020-12-20 10:56:07 所属栏目:Python 来源:网络整理
导读:当工作碰到需要将几个excel合并时,比如一个表,收集每个人的个人信息,陆续收回来就是十几张甚至几十张表,少了还好解决,但是很多的话就不能一个一个去复制了,这时候就想到了python,Python大法好

当工作碰到需要将几个excel合并时,比如一个表,收集每个人的个人信息,陆续收回来就是十几张甚至几十张表,少了还好解决,但是很多的话就不能一个一个去复制了,这时候就想到了python,Python大法好啊。短短100行代码解决,无论几十张,几百张表,瞬间搞定。

首先需要安装两个模块:xlrd(读取excel),xlsxwriter(写入excel)

pip install xlrd
pip install xlsxwriter

安装好以后,直接上代码。如下:

?

 1 # -*- coding: UTF-8 -*-
 2 # Filename : Merge_excel.py
 3 # author by : Awrrays
 4 
 5 import xlrd,xlsxwriter
 6 
 7 # 打开表格
 8 def openxls(file):
 9     try:
10         fx = xlrd.open_workbook(file)
11         return fx
12     except Exception as e:
13         print(读取文件错误,错误为:{0}.format(e))
14 
15 # 获取所有sheet
16 def getsheets(fx):
17     return fx.sheets()
18 
19 # 获取某个sheet的行数
20 def getrows(fx,sheet_num):
21     table = fx.sheets()[sheet_num]
22     rows = table.nrows
23     return rows
24 
25 # 获取某个文件的内容并返回所有行的内容
26 def getdump(fl,sheet_num):
27     fx = openxls(fl)
28     table = fx.sheet_by_name(sheet_name[sheet_num])
29     row_num = getrows(fx,sheet_num)
30     row_len = len(rows)
31     for row in range(0,row_num):
32         data = table.row_values(row)
33         rows.append(data)
34     dump.append(rows[row_len:])
35     return dump
36 # 定义要合并的所有文件
37 allxls = ["E:/test/test1.xlsx",E:/test/test2.xlsx,E:/test/test3.xlsx]
38 # 定义合并后的文件
39 endxls = "E:/test/test.xlsx"
40 
41 # 存储一个sheet的结果
42 sheet_value = []
43 # 存储各sheet的名称
44 sheet_name = []
45 # 存储一行内容
46 rows = []
47 # 存储所有读取的结果
48 dump = []
49 
50 # 读取第一个待读文件,获取sheet数
51 fx = openxls(allxls[0])
52 sheets = getsheets(fx)
53 x = 0
54 for sheet in sheets:
55     sheet_name.append(sheet.name)
56     sheet_value.append([])
57     x += 1
58 
59 # 依次读取各sheet的内容
60 for sheet_num in range(0,x):
61     # 依次获取每个文件当前sheet的内容
62     for fl in allxls:
63         print(正在读取文件{0}的第{1}个标签.....format(fl,sheet_num))
64         dump = getdump(fl,sheet_num)
65     sheet_value[sheet_num].append(dump)
66 
67 file_num = len(allxls)
68 endvlue = []
69 
70 # 获取各sheet的内容
71 def get_sheet_value(k):
72     for z in range(k,k+file_num):
73         endvlue.append(sheet_value[0][0][z])
74     return endvlue
75 
76 # 打开合并完成后的文件
77 wb = xlsxwriter.Workbook(endxls)
78 # 创建一个工作表
79 ws = wb.add_worksheet()
80 polit = 0
81 line_num = 0
82 # 依次遍历每个sheet中的内容
83 for s in range(0,x * file_num,file_num):
84     file_value = get_sheet_value(s)
85     table_value = file_value[polit:]
86     # 将每一个sheet中的内容写入新文件
87     for a in range(0,len(table_value)):
88         # 将sheet行写入到新文件
89         for b in range(0,len(table_value[0])):
90             # 将每一行的内容写入新文件
91             for c in range(0,len(table_value[0][0])):
92                 data = table_value[a][b][c]
93                 ws.write(line_num,c,data)
94             line_num += 1
95     # 设置分隔点
96     polit = len(file_value)
97 wb.close()

?

ok,最后结果:

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读