简体   繁体   English

如何将excel表保存到原始工作簿?

[英]How to save excel sheet to the original workbook?

I have a function like this:我有一个这样的功能:

def DuplicateEachRow():
        import pandas as pd
        import pathlib
        full_path = str(pathlib.Path().absolute()) + '\\' + new_loc

        df = pd.read_excel(full_path, header=None, sheet_name='Sheet3')
        print(df)

        # duplicate the rows:
        dup_df = pd.concat([df, df], ignore_index=True)

        # using openpyxl
        with pd.ExcelWriter(new_loc) as writer:
            dup_df.to_excel(writer)

and I need to keep this same functionality, but instead of writing that one sheet to a new file.我需要保持相同的功能,而不是将一张纸写入新文件。 I need to edit that one particular sheet and save it back to my workbook that has other sheets.我需要编辑那个特定的工作表并将其保存回我有其他工作表的工作簿。

EDIT (more explanation): I have 4 sheets in a workbook and in just one sheet (Sheet3) I need to use the functionality above and then save it back to a workbook.编辑(更多解释):我在工作簿中有 4 张工作表,只有一张工作表(Sheet3)我需要使用上面的功能,然后将其保存回工作簿。

This doesn't work either, specifying the sheet name when I save:这也不起作用,在我保存时指定工作表名称:

def DuplicateEachRow():
        import pandas as pd
        import pathlib
        full_path = str(pathlib.Path().absolute()) + '\\' + new_loc

        df = pd.read_excel(full_path, header=None, sheet_name='GTL | GWL Disclosures')
        print(df)

        # duplicate the rows:
        dup_df = pd.concat([df, df], ignore_index=True)

        # using openpyxl
        with pd.ExcelWriter(new_loc) as writer:
            dup_df.to_excel(writer, sheet_name='GTL | GWL Disclosures')

To add a news sheet in the same excel you have to open the file in mode append.要在同一个 Excel 中添加新闻表,您必须以追加模式打开文件。 Have a look at the code below:看看下面的代码:

def DuplicateEachRow():
    import pandas as pd
    import pathlib
    full_path = str(pathlib.Path().absolute()) + '\\' + new_loc

    df = pd.read_excel(full_path, header=None, sheet_name='GTL | GWL Disclosures')
    print(df)

    # duplicate the rows:
    # keep the index, so you can sort the rows after
    dup_df = pd.concat([df, df])
    #sort the rows by the index so you have the duplicate one just after the initial one
    dup_df.sort_index(inplace=True)

    # using openpyxl
    #open the file in append mode 
    with pd.ExcelWriter(new_loc, mode='a') as writer:
        #use a new name for the new sheet
        #don't save the header (dataframe columns names) and index (dataframe row names) in the new sheet  
        dup_df.to_excel(writer, sheet_name='Sheet3', header=None, index=None)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Python合并来自不同工作簿的多个同名excel工作表并保存到新的excel工作簿中 - How to merge multiple excel sheet with the same name from different workbooks and save into a new excel workbook using Python 如何从python中保存excel工作簿 - How to save an excel workbook from within python 如何使用openpyxl在普通模式下保存Excel工作簿? - How to save excel workbook in normal mode with openpyxl? 在 Python 中保存 Excel 工作簿 - Save excel workbook in Python 用python加密excel工作簿表 - encrypting excel workbook sheet with python 如何使用 python 将 excel 工作簿中的内容复制到另一个工作簿而不丢失 excel 格式 - How to copy contents from a sheet of an excel workbook to another workbook without loosing the excel formatting using python 如何在 Python 中将 Excel 工作表复制到另一个工作簿 - How to copy over an Excel sheet to another workbook in Python 如何 append 列出 python 中现有 excel 工作簿(同一张表)的列表 - How to append a list to an existing excel workbook (same sheet) in python 如何使用python将Excel工作表复制到具有相同格式的另一个工作簿 - How to copy excel sheet to another workbook with same format using python 如何在同一工作簿中创建指向不同 Excel 工作表的超链接 - How to create a hyperlink to a different Excel sheet in the same workbook
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM