Home > Back-end >  Inserting data into xlsx
Inserting data into xlsx

Time:03-23

I'm running a python script and the result of it is a group of values. Let's say the result is a unique date and time.

date = now.strftime("%d/%m/%Y")
time = now.strftime("%H:%M:%S")

I would like to write down the data into xlsx file, but the problem is that the data is rewrite every single time the scrip is done.

How should I write the date to add it into the xlsx and not to rewrite the first line of it? That's the code I am using and I'm not sure how to change it.

worksheet.write(1, 0, date)
worksheet.write(1, 1, time)

The result I would like to get at the end should be something like following:

Date         Time
20/03/2022   00:24:36
20/03/2022   00:55:36
21/03/2022   15:24:36
22/03/2022   11:24:36
23/03/2022   22:24:36

CodePudding user response:

You can open the excel in append mode and then keep inserting data. Refer below snippet:

with pd.ExcelWriter("existing_file_name.xlsx", engine="openpyxl", mode="a") as writer:
    df.to_excel(writer, sheet_name="name")

CodePudding user response:

The simplest way is to convert your data into a Pandas dataframe, then you can easily convert your dataframe into xlxs file by using a simple command like. df.to_excel("filename.xlxs")

  • Related