简体   繁体   中英

Adding row titles for Excel file using Python

hoping someone could guide me in solving something I am stuck on. Below is the code I have created to export specific columns from an excel file and to save the exported file in a specific location on the drive. What I am stuck is on adding rows to the exported excel file.

import tkinter as tk
from tkinter import filedialog
import panda as pd

root = tk.Tk()
root.withdraw()

file_path = filedialog.askopenfilename()

df.pd.read_excel(file_path, usecols=['date','time']

#below attempts would have been inputted at this point before the save function 

path_to_save = filedialog.asksaveasfilename(defaulttextextension='.xlsx)
df.to_excel(path_to_save, index=False)

I have tried doing this with: df.insert('Exported By:') - #this removes the data I have extracted and saves a blank excel file.

I went on and tried:

data = {'Exported by:':['']} #the idea for the blank space is because I want to be able to print out the excel file and manually write the item number as it changes for each excel file
df = pd.DataFrame(data) #But this didn't work and gave me an error

I'm stuck on this problem and can't seem to find a way to get this solved and not sure if I am doing something wrong within the code, any suggestions would be great!

The original file dataset: enter image description here

The exported file dataset: enter image description here

In image above I also added the addition of the rows that I would like to add to the above script.

I have found a solution to this using the df.loc[] = new row function, where I defined a new row. I am also trying to merge specific cells together and have been trying to use the df.merge_cells function but get an error saying ''DataFrame' object attribute 'merge_cells'' - slightly stuck on solving this one if anyone has an suggestions?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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