在工作中经常遇到需要打开许多个excel表格,然后合并的需求,合并的同时要求格式必须原汁原味的保留。利用VBA代码可以比较轻松的解决,现在我们来看Python中如何实现。

上代码:

from openpyxl import Workbook
from win32com.client import Dispatch
import os
import datetime def copy_excel_file(source_file_list, destination_file):
run_app = Dispatch('Excel.Application')
run_app.Visible = False # 改为True可以看到excel的打开窗口 for file in source_file_list:
source_workbook = run_app.Workbooks.Open(Filename=file)
destination_workbook = run_app.Workbooks.Open(Filename=destination_file) source_workbook.Worksheets(1).Copy(Before=destination_workbook.Worksheets(1))
destination_workbook.Close(SaveChanges=True) run_app.Quit() class ParameterGenerator: def __init__(self):
# self.directory_path = directory_path
self.file_lists = [] def creat_xlsx(self, directory_path):
obj = Workbook()
if not os.path.exists(directory_path + os.sep + 'joined'):
os.mkdir(directory_path + os.sep + 'joined')
date = str(datetime.datetime.today())[0:10]
obj.save(directory_path + os.sep + 'joined' + os.sep + 'joined {}.xlsx'.format(date)) def get_file_list(self, directory_path):
entry_lists = os.scandir(directory_path)
for entry_list in entry_lists:
if entry_list.is_file():
if '~$' not in entry_list.path:
self.file_lists.append(entry_list.path)
return self.file_lists def run(self, directory_path):
file_lists = self.get_file_list(directory_path)
self.creat_xlsx(directory_path)
destination_file = str(self.get_file_list(directory_path + os.sep + 'joined')[-1])
file_lists.pop(-1)
return file_lists, destination_file if __name__ == "__main__":
directory_path = r'D:\Excel目录'
param = ParameterGenerator()
source_file_list, destination_file = param.run(directory_path)
copy_excel_file(source_file_list, destination_file)

输出是文件夹下新建一个’joined‘的文件夹,里面有一个合并后的文件’joined xxxx-xx-xx.xlsx’,如下:

目前发现有两个需要注意的问题:

1. 需要合并的文件中不能有隐藏的表格,否则,会跳过该文件;

2. 文件名中不可以字符意外的标记,比如括号之类的。

最后,调用接口的速度有点慢,以后有机会还是看openpyxl是否可以实现一下,含格式的合并。xlwings是类似的实现,估计速度也差不多的慢。

Python - 调用接口合并文件夹下多个Excel表的更多相关文章

  1. Python win32com模块 合并文件夹内多个docx文件为一个docx

    Python win32com模块 合并文件夹内多个docx文件为一个docx #!/usr/bin/env python # -*- coding: utf-8 -*- from win32com. ...

  2. Python——合并指定文件夹下的所有excel文件

    前提:该文件夹下所有文件有表头且具有相同的表头. import glob # 同下 from numpy import * #请提前在CMD下安装完毕,pip install numppy impor ...

  3. python删除某一文件夹下的重复文件

    #2022-10-28 import hashlib import os import time def getmd5(filename): """ 获取文件 md5 码 ...

  4. Python脚本:删除文件夹下的重复图片,实现图片去重

    近期在整理相册的时候,发现相册中有许多重复图片,人工一张张筛查删除太枯燥,便写下这个脚本,用于删除文件夹下重复的图片. 第一部分:判断两张图片是否相同 要查找重复的图片,必然绕不开判断两张图片是否相同 ...

  5. 批量将制定文件夹下的全部Excel文件导入微软SQL数据库

    以下代码将c:\cs\文件夹下的全部Excle中数据导入到SQL数据库 declare @query vARCHAR(1000) declare @max1 int declare @count1 i ...

  6. Python如何读取指定文件夹下的所有图像

    (1)数据准备 数据集介绍: 数据集中存放的是1223幅图像,其中756个负样本(图像名称为0.1~0.756),458个正样本(图像名称为1.1~1.458),其中:"."前的标 ...

  7. Python 循环删除指定文件夹下所有的.longtian类型文件

    # -*- coding: utf-8 -*- import os #遍历文件夹删除文件 def traversing_dir(rootDir): #遍历根目录 for root,dirs,files ...

  8. python实现压缩当前文件夹下的所有文件

    import os import zipfile def zipDir(dirpath, outFullName): ''' 压缩指定文件夹 :param dirpath: 目标文件夹路径 :para ...

  9. 合并文件夹里多个excel

    Sub 合并当前目录下所有工作簿的全部工作表() Dim MyPath, MyName, AWbName Dim Wb As workbook, WbN As String Dim G As Long ...

随机推荐

  1. shell脚本基础知识以及变量

    一.基础知识 1.shell脚本的格式注意事项 第一行(一般必须写明):指定脚本使用的shell(若不写明也不影响脚本的执行,系统会自动以sh解析脚本)."#!/bin/bash" ...

  2. CSRF的几种防御方法的利弊分析

    本文直接从防御方式开始讨论,防御CSRF有4种方法: 使用POST替代GET 检验HTTP Referer 验证码 Token 使用POST替代GET 一些程序员在开发的时候都是用GET.POST通用 ...

  3. 常见SQL语句和SQL基础知识

    引自:http://blog.csdn.net/u012467492/article/details/46790205 SQL语句考察(一) 1.查询出每门课都大于80 分的学生姓名 name   k ...

  4. nginx 安装教程

    Nginx 安装教程 本教程在CentOS6.7中安装nginx 1.8.0,Nginx中加入了taobao的concat模块,nginx-upload-module模块,pcre 以及nginx-u ...

  5. Aplayer搭配Metingjs音乐插件的使用

    Aplayer搭配MetingJS音乐插件的使用 1. Aplayer和MetingJ的介绍 Aplayer官网文档:https://aplayer.js.org/#/ Metingjs官网文档:ht ...

  6. A - A FZU - 2205

    A - A FZU - 2205 一个国家有 N 个城市,国王不希望国家中存在三个城市之间能够互相直接到达,但道路要求尽可能的多,道路是双向边,且无重边无自环. 国王希望你最好能解决这个问题.求最多存 ...

  7. L - Subway(最短路spfa)

    L - Subway(最短路spfa) You have just moved from a quiet Waterloo neighbourhood to a big, noisy city. In ...

  8. flink 一分钟入门篇

    1. 业务说:“…… bulabula……,这个需求很简单,怎么实现我不管?” 面对霸气侧漏的业务需求,由于没有大数据知识储备,咱心里没底,咱也不敢问,咱也不敢说,只能静下来默默储备.默默寻觅解决方案 ...

  9. 多线程学习笔记(四)---- Thread类的其他方法介绍

    一.wait和 sleep的区别 wait可以指定时间也可以不指定时间,而sleep必须指定时间: 在同步中时,对cpu的执行权和锁的处理不同: wait:释放执行权,释放锁:释放锁是为了别人noti ...

  10. es6声明一个类

    js语言的传统方式是通过定义构造函数,生成心得对象.是一种基于原型的面向对象系统.在es6中增加了class类的概念,可以使用class关键字来声明一个类.之后用这个类来实例化对象. 构造函数示例 c ...