最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2020年03月31日 12:02:35   作者:Johnthegreat  
這篇文章主要介紹了Python調(diào)用接口合并Excel表代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

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

上代碼:

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)

輸出是文件夾下新建一個(gè)'joined‘的文件夾,里面有一個(gè)合并后的文件'joined xxxx-xx-xx.xlsx',如下:

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

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

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

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

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • django重新生成數(shù)據(jù)庫(kù)中的某張表方法

    django重新生成數(shù)據(jù)庫(kù)中的某張表方法

    今天小編就為大家分享一篇django重新生成數(shù)據(jù)庫(kù)中的某張表方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-08-08
  • Python文本到語(yǔ)音轉(zhuǎn)換庫(kù)pyttsx3的安裝及使用全面指南

    Python文本到語(yǔ)音轉(zhuǎn)換庫(kù)pyttsx3的安裝及使用全面指南

    這篇文章主要介紹了Python的text-to-speech庫(kù)pyttsx3的安裝、基本用法、配置語(yǔ)音屬性、支持的命令和功能以及高級(jí)用法,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2025-03-03
  • 最新評(píng)論

    竹山县| 曲水县| 巨野县| 双柏县| 冷水江市| 浠水县| 克什克腾旗| 玛曲县| 英山县| 安达市| 阜平县| 广宁县| 含山县| 云龙县| 垣曲县| 济源市| 县级市| 武夷山市| 镇雄县| 峨眉山市| 锡林浩特市| 左贡县| 海丰县| 射洪县| 罗定市| 揭西县| 游戏| 海安县| 五台县| 祥云县| 子长县| 崇仁县| 新民市| 雅安市| 泰州市| 黑龙江省| 平阳县| 湘乡市| 囊谦县| 安达市| 苏尼特右旗|