Python - 调用接口合并文件夹下多个Excel表
在工作中经常遇到需要打开许多个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表的更多相关文章
- Python win32com模块 合并文件夹内多个docx文件为一个docx
Python win32com模块 合并文件夹内多个docx文件为一个docx #!/usr/bin/env python # -*- coding: utf-8 -*- from win32com. ...
- Python——合并指定文件夹下的所有excel文件
前提:该文件夹下所有文件有表头且具有相同的表头. import glob # 同下 from numpy import * #请提前在CMD下安装完毕,pip install numppy impor ...
- python删除某一文件夹下的重复文件
#2022-10-28 import hashlib import os import time def getmd5(filename): """ 获取文件 md5 码 ...
- Python脚本:删除文件夹下的重复图片,实现图片去重
近期在整理相册的时候,发现相册中有许多重复图片,人工一张张筛查删除太枯燥,便写下这个脚本,用于删除文件夹下重复的图片. 第一部分:判断两张图片是否相同 要查找重复的图片,必然绕不开判断两张图片是否相同 ...
- 批量将制定文件夹下的全部Excel文件导入微软SQL数据库
以下代码将c:\cs\文件夹下的全部Excle中数据导入到SQL数据库 declare @query vARCHAR(1000) declare @max1 int declare @count1 i ...
- Python如何读取指定文件夹下的所有图像
(1)数据准备 数据集介绍: 数据集中存放的是1223幅图像,其中756个负样本(图像名称为0.1~0.756),458个正样本(图像名称为1.1~1.458),其中:"."前的标 ...
- Python 循环删除指定文件夹下所有的.longtian类型文件
# -*- coding: utf-8 -*- import os #遍历文件夹删除文件 def traversing_dir(rootDir): #遍历根目录 for root,dirs,files ...
- python实现压缩当前文件夹下的所有文件
import os import zipfile def zipDir(dirpath, outFullName): ''' 压缩指定文件夹 :param dirpath: 目标文件夹路径 :para ...
- 合并文件夹里多个excel
Sub 合并当前目录下所有工作簿的全部工作表() Dim MyPath, MyName, AWbName Dim Wb As workbook, WbN As String Dim G As Long ...
随机推荐
- wr720n v4 折腾笔记(一):安装Openwrt
好久没有写过文章了,今天先来个引子:wr720n v4版本安装openwrt 最近弄了个路由器wr720n,想抓包做点东西,于是就先从刷固件openwrt开始吧. 所需工具: 1.wr720n v4路 ...
- [模拟]Educational Codeforces Round 2A Extract Numbers
Extract Numbers time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...
- IDEA上传图片到tomcat服务器上
前端页面: JS代码: //选中图片 var form = document.getElementById("danxuan"); // 用表单来初始化 var formData ...
- python之面向对象的成员,方法,属性,异常处理
一.类的私有成员 1. 类中的私有成员是什么? 私有:只有满足一部分条件的才能使用 私有类的属性 私有对象的属性 私有方法 正常状态 class B: school_name = '老男孩教育' de ...
- python接口调用把执行结果追加到测试用例中
python操作excel的三个工具包如下,注意,只能操作.xls,不能操作.xlsx. xlrd: 对excel进行读相关操作 xlwt: 对excel进行写相关操作 xlutils: 对excel ...
- Python终端打印彩色文字
终端彩色文字 class Color_f: black = 30 red = 31 green = 32 yellow= 33 blue = 34 fuchsia=35 cyan = 36 white ...
- 《java编程思想》对象导论
1.抽象过程 所有编程语言都提供抽象机制.可以认为,人们所能够解决的问题的复杂性直接取决于抽象的类型和质量,所谓的'类型'是指“所抽象的是什么?”汇编语言是对底层机器的轻微抽象. java的基本 特性 ...
- POJ 1163 数字三角形
Portal:http://poj.org/problem?id=1163 DP经典题,IOI94考题,在各大OJ上都有 #include<iostream> #include<al ...
- 物体的三维识别与6D位姿估计:PPF系列论文介绍(三)
作者:仲夏夜之星 Date:2020-04-08 来源:物体的三维识别与6D位姿估计:PPF系列论文介绍(三) 文章“A Method for 6D Pose Estimation of Free-F ...
- Codeforces Round #629 (Div. 3)
A. Divisibility Problem time limit per test 1 second memory limit per test 256 megabytes input stand ...