色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術(shù)文章
文章詳情頁

Python調(diào)用接口合并Excel表代碼實(shí)例

瀏覽:110日期:2022-07-31 15:31:54

在工作中經(jīng)常遇到需要打開許多個excel表格,然后合并的需求,合并的同時要求格式必須原汁原味的保留。利用VBA代碼可以比較輕松的解決,現(xiàn)在我們來看Python中如何實(shí)現(xiàn)。

上代碼:

from openpyxl import Workbookfrom win32com.client import Dispatchimport osimport 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_fileif __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調(diào)用接口合并Excel表代碼實(shí)例

Python調(diào)用接口合并Excel表代碼實(shí)例

目前發(fā)現(xiàn)有兩個需要注意的問題:

1. 需要合并的文件中不能有隱藏的表格,否則,會跳過該文件;

2. 文件名中不可以字符意外的標(biāo)記,比如括號之類的。

最后,調(diào)用接口的速度有點(diǎn)慢,以后有機(jī)會還是看openpyxl是否可以實(shí)現(xiàn)一下,含格式的合并。xlwings是類似的實(shí)現(xiàn),估計速度也差不多的慢。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: python
相關(guān)文章:
主站蜘蛛池模板: 亚洲高清国产拍精品影院 | 成人做爰免费网站 | 一级一毛片a级毛片欧美 | 成人男女18免费o | 亚洲综合91 | 亚洲一一在线 | 日本三级一区二区三区 | 欧美日韩一区二区综合在线视频 | 一级毛片成人免费看免费不卡 | 97国产免费全部免费观看 | 欧美高清性色生活片免费观看 | 久久国产一区二区 | 国产亚洲精品午夜一区 | 日本特级淫片免费看 | 久久久久久88色愉愉 | 真正免费一级毛片在线播放 | 99视频网| 免费一级特黄特色黄大任片 | 亚欧成人| 国产亚洲精品久久久久久久久激情 | 国产一区精品在线 | 日本一级在线观看 | 美女福利视频午夜在线 | 日日摸人人拍人人澡 | 久久爱青青草 | 久久99精品视频在线在线观看 | 久久夜色精品国产 | 写真片福利视频在线播放 | 国产精品久久大陆 | 国产美女在线精品亚洲二区 | 亚洲 欧美 国产 日韩 制服 bt | 亚洲视频在线观看网站 | 欧美视频www | 国产高清在线视频 | 颜值超高的女神啪啪 | 日本久草网| 国产91会所洗浴女技师按摩 | 国内精品99 | 亚洲欧洲国产成人精品 | avtt加勒比手机版天堂网 | 步兵精品手机在线观看 |