简体   繁体   English

使用 pandas 和 openxlpy 将 DF 写入现有的 Excel 文件

[英]Write DF to existing Excel file with pandas and openxlpy

I am trying to write a df to an existing work sheet.我正在尝试将 df 写入现有的工作表。 Initially the programme opens a dialog box to select a new sheet name to be added, then find the path of the excel sheet.最初程序会打开一个对话框给 select 一个要添加的新工作表名称,然后找到 excel 工作表的路径。

#reload window to select new sheet name
root = tk.Tk()
root.geometry('500x200+350+150')
root.configure(bg='#072462')

#set buttons
label_date = Label(root, text='Input New Sheet Name', bg='#072462', fg='white')
ws_new_title = Entry(root, width=45, bg='#0A1944', fg='white', highlightthickness=0, borderwidth=0)
ws_new_title.insert(0, 'YYYY-MM-DD') #insert guide text in box
button_ws_name = Button(root, width=20, text='Select Export Excel File', command=root.quit)
button_ws_name.configure(fg='#C51E42', bg='#06122f', borderwidth=5)

label_date.pack()
ws_new_title.pack()
button_ws_name.pack(pady=30)
root.mainloop()

Code for dialog box to find excel path (working)用于查找 excel 路径的对话框代码(工作)

#find and save to new excel sheet based on a input of name
root = tk.Tk()
root.title('Great Britain Basketball')
root.withdraw()
file_path_export = filedialog.askopenfilename(initialdir='/Desktop/', title='Select Player Tracking document')
root.update()
export_fn = file_path_export

code to open workbook and add new sheet (think this. might be wrong)打开工作簿并添加新工作表的代码(认为这个。可能是错误的)

book = load_workbook(export_fn)
writer = pd.ExcelWriter(export_fn, engine='openpyxl')
writer.book = book
writer.sheets = dict((ws.title, ws) for ws in book.worksheets)

finally to write df (full_append) to excel最后将 df (full_append) 写入 excel

full_append.to_excel(writer, startrow=len(df)+1, index=False, header=False, sheet_name='Season Data') #working -write to exsisting sheet and append to bottom row
full_append.to_excel(writer, sheet_name=ws_new_title, index=False) #not working - write to same sheet but on seperate sheet taking name from input
writer.save()

So the main issue is the programme is not adding a new sheet in the workbook using a prescribed sheet name (ws_new_title variable), and writing the df to it.所以主要问题是程序没有使用规定的工作表名称(ws_new_title 变量)在工作簿中添加新工作表,并将 df 写入其中。 Thank you谢谢

Managed to find the syntax error.设法找到语法错误。 After setting the entry button设置进入按钮后

ws_new_title = Entry(root, width=45, bg='#0A1944', fg='white', highlightthickness=0, borderwidth=0)

I needed to change this entry to a string variable before using as a new sheet name in existing Excel sheet.在将其用作现有 Excel 工作表中的新工作表名称之前,我需要将此条目更改为字符串变量。 To do this I used a.get() function为此,我使用了 a.get() function

#change entry to a string
ws_name = ws_new_title.get()

Then finally append df to 'Season Data' and save to a new sheet based on date然后最后 append df 到“季节数据”并根据日期保存到新工作表

#write to excel workbook
book = load_workbook(export_fn)
writer = pd.ExcelWriter(export_fn, engine='openpyxl')
writer.book = book
writer.sheets = dict((ws.title, ws) for ws in book.worksheets)

full_data_table.to_excel(writer, sheet_name=ws_name, index=False) #working but need to update name based on input
full_data_table.to_excel(writer, startrow=len(df)+1, index=False, header=False, sheet_name='Season Data') #working -write to exsisting sheet and append to bottom row

writer.save()
writer.close()

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM