python-将多个表格的信息合并到一个表格中

1、环境

代码运行环境:python3.7

相关的库:xlrd、xlwt

2、目的

通过xlrd库读取各个表格的数据,通过xlwt库将读取到的数据写入到一个表格中。

3、实现

在工程目录下,有一个test目录,存放的是待合并的表格,输出表格为merge.xls,就输出在当前工程目录下。每个合并的表格中我只需要"body"这个sheet,而且这个sheet中的数据格式都是按照下面的格式存放的。

 实现的代码如下:

 1 # encoding: utf-8
 2 
 3 '''
 4 本代码的作用是将多个表格合并为一个表格。
 5 '''
 6 
 7 import os
 8 import xlrd
 9 import xlwt
10 import logging
11 
12 # 设置logging.basicConfig()方法的参数和配置logging.basicConfig函数
13 FORMAT = '[%(funcName)s: %(lineno)d]: %(message)s'
14 LEVEL = logging.INFO
15 logging.basicConfig(level = LEVEL, format=FORMAT)
16 
17 excel_content = []
18 output_file = './merge.xls'
19 
20 # 打开表格,获取信息
21 def get_obj_list(dir_name):
22     filelist = os.listdir(dir_name)
23     for item in filelist :
24         item = dir_name + item
25         if os.path.isfile(item) and (item[-4:] == '.xls' or item[-5:] == '.xlsx' or item[-5:] == '.xlsm'):
26             if item.find("$") != -1:
27                 continue
28             merge_excel(item)
29         elif os.path.isdir(item):    
30             item = item + '/'
31             get_obj_list(item)
32 
33 
34 # 获取单个表格的信息
35 def merge_excel(excelName):
36     excelfd = xlrd.open_workbook(excelName)
37     for sheet in excelfd.sheet_names():
38         if sheet == 'body':
39             print (excelName)
40             sheet_content = excelfd.sheet_by_name(sheet)
41             header = sheet_content.cell(0, 0).value
42             if header == u'高校名称':   # 去掉标题行
43                 row = 1
44             else:
45                 row = 0
46             while row < sheet_content.nrows:
47                 college    = sheet_content.cell(row, 0).value
48                 institute  = sheet_content.cell(row, 1).value
49                 built_time = sheet_content.cell(row, 2).value
50                 overview   = sheet_content.cell(row, 3).value
51                 item = [college, institute, built_time, overview]
52                 excel_content.append(item)
53                 row += 1
54 
55 
56 # 将获取到的表格信息保存到一个表格中
57 def save_info():
58     workbook = xlwt.Workbook(encoding = 'ascii')
59     worksheet = workbook.add_sheet('merge_info')
60     style = xlwt.XFStyle() # 初始化样式
61     font = xlwt.Font() # 为样式创建字体
62     font.name = 'Times New Roman' 
63     font.bold = True # 黑体
64     font.underline = True # 下划线
65     font.italic = True # 斜体字
66     style.font = font # 设定样式
67     worksheet.write(0, 0, '高校名称')
68     worksheet.write(0, 1, '学院名称')
69     worksheet.write(0, 2, '成立时间')
70     worksheet.write(0, 3, '人工智能学院、人工智能研究院建设情况')
71 
72     for i, item in enumerate(excel_content):
73         for j in range(4):  #多添加一列(序号)
74             worksheet.write(i+1, j, item[j])
75     workbook.save(output_file) # 保存文件
76         
77 
78 if __name__ == "__main__":
79 
80     if os.path.exists(output_file):
81         os.remove(output_file)
82 
83     get_obj_list('./test/')
84     save_info()

4、输出结果

这是我手动调整过表格格式的结果。至此,需要实现的功能都实现了。

posted @ 2020-04-04 16:04  zhengcixi  阅读(9533)  评论(0编辑  收藏  举报
回到顶部