Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
合并多个excel文件并保持格式(Python)_Python_Pandas_Merge_Formatting - Fatal编程技术网

合并多个excel文件并保持格式(Python)

合并多个excel文件并保持格式(Python),python,pandas,merge,formatting,Python,Pandas,Merge,Formatting,我有合并excel文件的简单代码。但它一直在丢失格式。有什么建议吗 谢谢你的回复 import numpy as np import pandas as pd import openpyxl import os import glob all_names = [] for x in os.listdir(r'C:\<directory to excel files>'): if x.endswith(".xlsx"): all_names.append(x

我有合并excel文件的简单代码。但它一直在丢失格式。有什么建议吗

谢谢你的回复

import numpy as np
import pandas as pd
import openpyxl
import os
import glob

all_names = []

for x in os.listdir(r'C:\<directory to excel files>'):
    if x.endswith(".xlsx"):
        all_names.append(x[:-5])
        print(all_names)

all_data = pd.DataFrame()
for f in glob.glob("*.xlsx"):
    df = pd.read_excel(f)
    all_data = all_data.append(df,ignore_index=True)

appended_df = pd.concat((all_data), keys=(all_names))
appended_df.to_excel(r"C:\<directory for merged file>")
将numpy导入为np
作为pd进口熊猫
导入openpyxl
导入操作系统
导入glob
所有_名称=[]
对于os.listdir(r'C:\')中的x:
如果x.endswith(“.xlsx”):
所有_名称。追加(x[:-5])
打印(所有名称)
all_data=pd.DataFrame()
对于glob.glob(“*.xlsx”)中的f:
df=pd.read\u excel(f)
all_data=all_data.append(df,ignore_index=True)
追加的_df=pd.concat((所有_数据),键=(所有_名称))
在excel(r“C:\”)中追加

一种保留格式的方法是将DF附加到现有Excel文件中,类似这样

def append_df_to_excel(filename, df, sheet_name='Sheet1', startrow=None,
                       truncate_sheet=False,
                       **to_excel_kwargs):

    # ignore [engine] parameter if it was passed
    if 'engine' in to_excel_kwargs:
        to_excel_kwargs.pop('engine')

    writer = pd.ExcelWriter(filename, engine='openpyxl')

    try:
        # try to open an existing workbook
        writer.book = load_workbook(filename)

        # get the last row in the existing Excel sheet
        # if it was not specified explicitly
        if startrow is None and sheet_name in writer.book.sheetnames:
            startrow = writer.book[sheet_name].max_row

        # truncate sheet
        if truncate_sheet and sheet_name in writer.book.sheetnames:
            # index of [sheet_name] sheet
            idx = writer.book.sheetnames.index(sheet_name)
            # remove [sheet_name]
            writer.book.remove(writer.book.worksheets[idx])
            # create an empty sheet [sheet_name] using old index
            writer.book.create_sheet(sheet_name, idx)

        # copy existing sheets
        writer.sheets = {ws.title: ws for ws in writer.book.worksheets}
    except FileNotFoundError:
        # file does not exist yet, we will create it
        pass

    if startrow is None:
        startrow = 0

    # write out the new sheet
    df.to_excel(writer, sheet_name, startrow=startrow, **to_excel_kwargs)

    # save the workbook
    writer.save()

我不断得到错误:writer=
pd.ExcelWriter(文件名,engine='openpyxl')
它说抽象类'ExcelWriter'和抽象方法实例化似乎是pylint的一个已知错误。尝试使用pylint指令在上下文管理器中设置它,就像他们建议的那样:
使用pd.ExcelWriter(filename,engine='openpyxl')作为编写器:#pylint:disable=abstract class实例化
好的,不再有错误,但它不会显示任何格式。。