Как мне присоединиться к файлам xlsm? - PullRequest
0 голосов
/ 27 мая 2020

Я пытаюсь присоединить несколько файлов xlsm. И этот код работает, чтобы присоединиться к ним, но результатом является не файл xlsm, а вместо него xlsx, я попытался использовать xlsm в переменной сохранения, но не сработало.

import os
import pandas as pd
from openpyxl import load_workbook

#JOIN ALL SOIL/TABLE.XLSX
paths= ["C:/Users/hugo/Desktop/before/"]
save = "C:/Users/hugo/Desktop/Protocol_Request.xlsx"
issue = "C:/Users/hugo/Desktop/error_table.txt"

def getListOfFiles(dirName):
    # create a list of file and sub directories 
    # names in the given directory 
    listOfFile = os.listdir(dirName)
    allFiles = list()

    # Iterate over all the entries
    for entry in listOfFile:
        # Create full path
        fullPath = os.path.join(dirName, entry)

        # If entry is a directory then get the list of files in this directory 
        if os.path.isdir(fullPath):
            allFiles = allFiles + getListOfFiles(fullPath)

        else:
            allFiles.append(fullPath)        

        #print (allFiles)
    return allFiles

def findTable(path):
    if (path.endswith('xlsm')):
        df = load_workbook(path,keep_vba=True)
        df['Protocol request']
        #df = pd.DataFrame(wb)
        try:
                df.to_excel(save)
        except:
            with open(issue, 'a') as f:
                f.write(path + '\n')

for path in paths:
    dirs_ = getListOfFiles(path)
    print('------------- All dirs with issue ----------------\n')
    for dir_ in dirs_:
        findTable(dir_)

Результат в Excel выглядит следующим образом

...