在工作中经常遇到需要打开许多个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. [LeetCode] 936. Stamping The Sequence 戳印序列

    You want to form a `target` string of lowercase letters. At the beginning, your sequence is target.l ...

  2. 【Pytest03】全网最全最新的Pytest框架fixture应用篇(1)

    fixtrue修饰器标记的方法通常用于在其他函数.模块.类或者整个工程调用时会优先执行,通常会被用于完成预置处理和重复操作.例如:登录,执行SQL等操作. 完整方法如下:fixture(scope=' ...

  3. Api接口幂等设计

    1,Api接口幂等设计,也就是要保证数据的唯一性,不允许有重复. 例如:rpc 远程调用,因为网络延迟,出现了调用了2次的情况. 表单连续点击,出现了重复提交. 接口暴露之后,会被模拟请求工具(Jem ...

  4. Mybatis 小记

    1,mybatis 中 $ # 区别 mybatis 动态传参的两种方式 #{ }在动态解析的时候,会将#{ } 解析为一个预编译阶段的一个标记符号?,在预处理阶段才会替换 ${ }在动态解析的时候, ...

  5. 美国NOAA/AVHRR遥感数据

    1.美国国家海洋和大气管理局(National Oceanic and Atmospheric Administration,NOAA) 美国国家海洋和大气管理局隶属于美国商业部下属的科技部门,主要关 ...

  6. ovirt 替换自主签署证书

    需求我自己写了一个python后台,添加上了ovirt 引擎web上,如图 但第一次访问时需要,需要接受两次不安全连接,ovirt  web使用https,我往里面加http,加不进去. 只能同样使用 ...

  7. Java 虚拟机运行时数据区

    写在前面 本文描述的有关于 JVM 的运行时数据区是基于 HotSpot 虚拟机. 概述 JVM 在执行 Java 程序的过程中会把它所管理的内存划分为若干个不同的数据区域.这些区域都有各自的用途,以 ...

  8. Pytest系列(16)- 分布式测试插件之pytest-xdist的详细使用

    如果你还想从头学起Pytest,可以看看这个系列的文章哦! https://www.cnblogs.com/poloyy/category/1690628.html 前言 平常我们功能测试用例非常多时 ...

  9. 中阶 d06.1 cookie && session && jsp介绍

    ##Cookie > 饼干. 其实是一份小数据, 是服务器给客户端,并且存储在客户端上的一份小数据 ### 应用场景 > 自动登录.浏览记录.购物车. ###为什么要有这个Cookie & ...

  10. Struts2-学习笔记系列(8)-异常处理

    后台抛出自定义异常 public String execute() throws Exception { if (getUser().equalsIgnoreCase("user" ...