How do I add information to an excel sheet without deleting the rest in Python?

て烟熏妆下的殇ゞ 提交于 2021-02-08 09:18:22

问题


Problem

I need to make a python script that can export information to excel sheet without deleting sheets old information or sheets. See Pseudocode

Context:

This function is to be used in conjunction with a web scraper. And should hopefully, be able to extend data to an existing sheet, create a new sheet, and create a new file.

With the help of other questions, I have come really close: How to save a new sheet in an existing excel file, using Pandas? But I want the possibility to extend on existing sheets.

Pseudocode

def OutputDataToExcel(Filename,Sheetname,df): #df is a pandas dataframe
    If Filename and Sheetname exists:
        extend the Sheetname with df (no header)
    elif Filename exists:
        create new sheet with Sheetname
        add df (with header)
    else:
    create new excelfile with Filename
    create new sheet with Sheetname
    add df (with header)

My code so far

Some is out commented to focus on the problem

import openpyxl
import pandas as pd

def outputdata(Filename,Sheetname,df):
    #try:
    #will error if file or sheet cannot be found
    #And will extend information to the sheet
    xl1 = pd.read_excel(Filename,Sheetname)
    xl2 = pd.ExcelFile(Filename)
    wb=openpyxl.load_workbook(Filename)
    length = len(xl2.parse(Sheetname))
    writer = pd.ExcelWriter(Filename,sheet_name = Sheetname , engine = "openpyxl")
    df.to_excel(writer,Sheetname, startrow=length+1, index=False, header = None)
    writer.book = wb

    writer.save()
    #writer.close()
    #except:
        #try:
            #will error if file cannot be found
            #and will create a new sheet with information
            #wb=openpyxl.load_workbook(Filename)
            #wb.openpyxl.create_sheet(Sheetname)
            #df.to_excel(writer,Sheetname, index=False)
            #writer.save()
            #writer.close()
        #except:
            #writer = pd.ExcelWriter(Filename,sheet_name = Sheetname  , engine = "openpyxl")
            #df.to_excel(writer,Sheetname, index= False)
            #writer.save()
            #writer.close()

The result

A new sheet is created with the name Sheetname1 and df is added to this sheet, insted of it being added to Sheetname. If I run it again a new sheet named Sheetname2 and df is then added here...

I hope someone can help me, it's my first question so, please respond both with help or critic on how to present the problem.


回答1:


I finally got it!! If anyone has any interest, here is the code:

import xlsxwriter
import pandas as pd

def outputdata(Filename,Sheetname,df):
    try:
        xlsx_file = pd.ExcelFile(Filename)
    except:
        xlsxwriter.Workbook(Filename)
        xlsx_file = pd.ExcelFile(Filename)

    writer = pd.ExcelWriter(Filename, engine='openpyxl')
    IsSheetThereAlready = False
    for sheet in xlsx_file.sheet_names:
        if sheet == Sheetname:
            df2 = xlsx_file.parse(sheet)
            df2.to_excel(writer,sheet_name= sheet, index=False)
            df.to_excel(writer,sheet_name= sheet, startrow=len(df2)+1, index=False, header=None)
            IsSheetThereAlready = True

        else:
            df2 = xlsx_file.parse(sheet)
            df2.to_excel(writer,sheet_name= sheet, index=False)


    if IsSheetThereAlready is False:
        df.to_excel(writer,sheet_name = Sheetname, index=False)

    writer.save()

    return


来源:https://stackoverflow.com/questions/52095254/how-do-i-add-information-to-an-excel-sheet-without-deleting-the-rest-in-python

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!