使用Python为Excel文件添加行标题



希望有人能指导我解决我遇到的问题。下面是我创建的代码,用于从excel文件导出特定列,并将导出的文件保存在驱动器上的特定位置。我一直在为导出的excel文件添加行。

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)

我试着用以下方法来做这件事:df.insert('Exported By:')-#这将删除我提取的数据,并保存一个空白的excel文件。

我继续尝试:

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

我被这个问题卡住了,似乎找不到解决这个问题的方法,也不确定我是否在代码中做错了什么,任何建议都会很好!

原始文件数据集:在此处输入图像描述

导出的文件数据集:在此处输入图像描述

在上面的图片中,我还添加了我想添加到上面脚本中的行。

我使用df.loc[] = new row函数找到了一个解决方案,在这里我定义了一个新行。我也在尝试将特定的单元格合并在一起,并一直在尝试使用df.merge_cells函数,但在说"DataFrame"对象属性"merge_cells"时遇到了一个错误-如果有人有建议,请稍微解决这个问题?

最新更新