How to add timestamp to excel file in Python?

If you're working with data in Python and need to track changes or monitor updates, adding a timestamp to your Excel files can be a game-changer. When we are working with large quantities of data, excel can be used to analyze when specific modifications or events occurred. This can be accomplished by including a timestamp in the Excel file which will tell when a particular modification has been done to a cell.

The modules required for adding timestamps to Excel files will be openpyxl and DateTime. In this article, we will see how to add timestamps and the modules used to do the same in detail.

Datetime module

A datetime module can be used to work with both the date and the time as there are no date or time data types in Python. Python already has the DateTime module built in, so we don't need to install it separately.

Date and time manipulation classes are available in the Python DateTime module. Dates, times, and periods of time are the main parameters of the methods included in these frameworks. Because Date and DateTime are objects in Python rather than texts or timestamps, any modifications you make to them will have the same effect.


Openpyxl is a Python library for reading and writing Excel files. Openpyxl is utilized by data scientists for data analysis, data replication, data mining, drawing charts, formatting sheets, and adding formulas, among other tasks.

Openpyxl will be used in the

  • Workbook − In openpyxl, a spreadsheet is depicted as a workbook. There are one or more papers in a workbook.

  • Sheet − A sheet is a single document made up of compartments used to organize data.

  • Cell − The intersection of a row and column is referred to as a cell. Typically denoted by A1, B5, etc.

  • Row − A row is a horizontal line indicated by a number (1,2, etc.).

  • Column − A column is a vertical line denoted by an uppercase letter (A, B, etc.).

Openpyxl can be installed using the pip command, and installation in a virtual environment is recommended. The syntax to install openpyxl using pip is given below −

pip install openpyxl

Steps to add timestamp to Excel file in Python

  • At first, Python must be installed on your computer. If Python is not already installed on your computer, you can download and install it from the official website. Once Python has been installed, you must install the "openpyxl" Python package. This can be done by opening a command prompt and inputting the following command −

pip install openpyxl
  • Now we have installed openpyxl module. The next step is to import “openpyxl” package and “datetime” module.

  • Use the “load_workbook()” method to open the Excel workbook in which you want to add the timestamp.

  • Using the “” method, we get the current date and time and add the following to A1 cell by using the “strftime()” method to format the date and the time.

  • At last, by using the “save()” method save the changes you made to the workbook. You can make changes to the following code for example, if you want to add a timestamp to a different cell or format the date and time in a different way, depending on your needs.

Program to add timestamp to a single cell

#Adding time stamp to a single cell
from datetime import datetime
from openpyxl import workbook
import time

#Open the desired workbook using openpyxl
wb = openpyxl.load_workbook('C:/Users/Tutorialspoint/Desktop/example1.xlsx')

#Select the workbook that is currently active
sheet =

# now we will get the current date and time
now =

# Add the date and time to a particular cell
sheet['A1'] = now.strftime("%Y-%m-%d %H:%M:%S")

# Save the changes to the workbook'C:/Users/Tutorialspoint/Desktop/example1.xlsx')


Program to add time stamp to a multiple cell

We will add time stamps to multiple cells with the time difference of 3 seconds by using sleep method.

import time
from openpyxl import Workbook
import datetime

# Open the workbook and select the sheet
wb = openpyxl.load_workbook('example.xlsx')

#Select the workbook that is currently active
sheet =
for i in range(10):
    #we will add timestamps to different cells after break of 3 seconds
    sheet.cell(row=1, column=1).value ='%Y-%m-%d %H:%M:%S')
    sheet.cell(row=2, column=1).value ='%Y-%m-%d %H:%M:%S')
    sheet.cell(row=3, column=1).value ='%Y-%m-%d %H:%M:%S')
    sheet.cell(row=4, column=1).value ='%Y-%m-%d %H:%M:%S')
    sheet.cell(row=5, column=1).value ='%Y-%m-%d %H:%M:%S')

# Save the changes to the workbook'C:/Users/Tutorialspoint/Desktop/example.xlsx')



In conclusion, adding timestamps to numerous cells in an Excel file using Python may be beneficial for tracking changes to a big quantity of data. This can be accomplished by adding timestamps to many rows. If you want to effortlessly add timestamps to numerous cells in an Excel file using Python, all you need to do is follow the procedures that are explained in detail in this article. We have also discussed the modules in detail that are used in the program.

Updated on: 31-May-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started