Python调用接口合并Excel表代码实例


Posted in Python onMarch 31, 2020

在工作中经常遇到需要打开许多个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',如下:

Python调用接口合并Excel表代码实例

Python调用接口合并Excel表代码实例

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

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

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

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python启动办公软件进程(word、excel、ppt、以及wps的et、wps、wpp)
Apr 09 Python
Python实现字符串格式化的方法小结
Feb 20 Python
对numpy 数组和矩阵的乘法的进一步理解
Apr 04 Python
利用python如何处理百万条数据(适用java新手)
Jun 06 Python
matplotlib调整子图间距,调整整体空白的方法
Aug 03 Python
python按时间排序目录下的文件实现方法
Oct 17 Python
django使用LDAP验证的方法示例
Dec 10 Python
Ubuntu下升级 python3.7.1流程备忘(推荐)
Dec 10 Python
Python列表切片操作实例总结
Feb 19 Python
Python3 Tensorlfow:增加或者减小矩阵维度的实现
May 22 Python
Python Spyder 调出缩进对齐线的操作
Feb 26 Python
python中pd.cut()与pd.qcut()的对比及示例
Jun 16 Python
Python如何批量获取文件夹的大小并保存
Mar 31 #Python
Django使用list对单个或者多个字段求values值实例
Mar 31 #Python
django实现模板中的字符串文字和自动转义
Mar 31 #Python
Python使用graphviz画流程图过程解析
Mar 31 #Python
Django模板之基本的 for 循环 和 List内容的显示方式
Mar 31 #Python
基于python实现计算且附带进度条代码实例
Mar 31 #Python
Django values()和value_list()的使用
Mar 31 #Python
You might like
基于文本的访客签到簿
2006/10/09 PHP
smarty基础之拼接字符串的详解
2013/06/18 PHP
php查询操作实现投票功能
2016/05/09 PHP
Thinkphp实现站点静态化的方法详解
2017/03/21 PHP
jscript之Open an Excel Spreadsheet
2007/06/13 Javascript
javascript引用对象的方法代码
2007/08/13 Javascript
js计算字符串长度包含的中文是utf8格式
2013/10/15 Javascript
ff下JQuery无法监听input的keyup事件的解决方法
2013/12/12 Javascript
jquery 字符串切割函数substring的用法说明
2014/02/11 Javascript
jQuery 1.9使用$.support替代$.browser的使用方法
2014/05/27 Javascript
js中split和replace的用法实例
2015/02/28 Javascript
JS中this上下文对象使用方式
2016/10/09 Javascript
使用jsonp实现跨域获取数据实例讲解
2016/12/25 Javascript
对于js垃圾回收机制的理解
2017/09/14 Javascript
jQuery+SpringMVC中的复选框选择与传值实例
2018/01/08 jQuery
解决Linux无法正常安装与卸载Node.js的方法
2018/01/19 Javascript
Element-ui自定义table表头、修改列标题样式、添加tooltip、:render-header使用
2019/04/11 Javascript
使用layui的layer组件做弹出层的例子
2019/09/27 Javascript
浅谈webpack和webpack-cli模块源码分析
2020/01/19 Javascript
Python跳出循环语句continue与break的区别
2014/08/25 Python
Python的Bottle框架中返回静态文件和JSON对象的方法
2015/04/30 Python
Python编程中NotImplementedError的使用方法
2018/04/21 Python
django 框架实现的用户注册、登录、退出功能示例
2019/11/28 Python
Python Opencv 通过轨迹(跟踪)栏实现更改整张图像的背景颜色
2020/03/09 Python
Python 实现打印单词的菱形字符图案
2020/04/12 Python
Python爬虫爬取博客实现可视化过程解析
2020/06/29 Python
Python 利用flask搭建一个共享服务器的步骤
2020/12/05 Python
使用canvas一步步实现图片打码功能的方法
2019/06/17 HTML / CSS
戴尔新加坡官网:Dell Singapore
2020/12/13 全球购物
俄语专业毕业生推荐信
2013/10/28 职场文书
教师简历自我评价
2014/02/03 职场文书
护理专业毕业生自荐书
2014/05/24 职场文书
新文化运动的口号
2014/06/21 职场文书
基层党员干部四风问题整改方向和措施
2014/09/25 职场文书
股东出资证明书范例
2014/10/04 职场文书
CSS3实现的文字弹出特效
2021/04/16 HTML / CSS