如何使用python合并多个excel文件?

如何使用python合并多个excel文件?,python,pandas,zipfile,Python,Pandas,Zipfile,我有一个压缩多个excel文件的代码,我想合并这些文件并在一个excel文件中显示它们的内容。你对如何编写这个函数有什么新的想法吗 还有,我的函数中的错误在哪里 当我运行代码时,它会崩溃并显示以下错误: AttributeError: 'ZipFile' object has no attribute 'seek' Traceback: File "F:\AIenv\lib\site-packages\streamlit\script_runner.py", line 333

我有一个压缩多个excel文件的代码,我想合并这些文件并在一个excel文件中显示它们的内容。你对如何编写这个函数有什么新的想法吗

还有,我的函数中的错误在哪里

当我运行代码时,它会崩溃并显示以下错误:

AttributeError: 'ZipFile' object has no attribute 'seek'
Traceback:
File "F:\AIenv\lib\site-packages\streamlit\script_runner.py", line 333, in _run_script
    exec(code, module.__dict__)
File "f:\AIenv\streamlit\app2.py", line 131, in <module>
    excel_file_merge(f)
File "f:\AIenv\streamlit\app2.py", line 77, in excel_file_merge
    archive = zipfile.ZipFile(zip_file_name, 'r')
File "c:\users\lt gm\appdata\local\programs\python\python37\Lib\zipfile.py", line 1222, in __init__
    self._RealGetContents()
File "c:\users\lt gm\appdata\local\programs\python\python37\Lib\zipfile.py", line 1285, in _RealGetContents
    endrec = _EndRecData(fp)
File "c:\users\lt gm\appdata\local\programs\python\python37\Lib\zipfile.py", line 259, in _EndRecData
    fpin.seek(0, 2)

错误出现在这一行
archive=zipfile.zipfile(zip文件名,'r')

这是否回答了您的问题@jtlz2不,我已经根据这篇文章关闭了zip文件,但仍然是相同的错误
import pandas as pd
import numpy as np
import zipfile
import os


# Excel file merge function
def excel_file_merge(zip_file_name):
    df = pd.DataFrame()
    archive = zipfile.ZipFile(zip_file_name, 'r')
    with zipfile.ZipFile(zip_file_name, "r") as f:
        for file in f.namelist():
            xlfile = archive.open(file)
        if file.endswith('.xlsx'):
            # Add a note indicating the file name that this dataframe originates from
            df_xl = pd.read_excel(xlfile, engine='openpyxl')
            df_xl['Note'] = file
            # Appends content of each Excel file iteratively
            df = df.append(df_xl, ignore_index=True)
    return df

# function in order to zipped files.
def get_all_file_path(directory):
    file_paths=[]
    for root,directories,files in os.walk(directory):
        for filename in files:
            filepath = os.path.join(root,filename)
            file_paths.append(filepath)
    return file_paths
    
file_paths = get_all_file_path(uploaded_file)
print("following files will be zipped: ")
for file_name in file_paths:
    print(file_name)
with zipfile.ZipFile("my _python_files.zip","w")as f:
    for file in file_paths:
        f.write(file)
f.close()
print("All Files Zipped successfully")
excel_file_merge(f)