Open In App

How to add timestamp to excel file in Python

Last Updated : 22 Nov, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will discuss how to add a timestamp to an excel file using Python.

Modules required

  • datetime: This module helps us to work with dates and times in Python.
pip install datetime
  • openpyxl: It is a Python library used for reading and writing Excel files.
pip install openpyxl
  • time: This module provides various time-related functions.

Stepwise Implementation

Step 1: Create the workbook object and select the active sheet:

wb = Workbook()
ws = wb.active

Step 2 (Optional): Write the heading in cell A1.

# Here column=1 represents column A and row=1 represents first row.
ws.cell(row=1, column=1).value = "Current Date and Time"

Step 3: Use the following command to get the current DateTime from the system.

time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')

The strftime() function is used to convert date and time objects to their string representation. It takes one or more inputs of formatted code and returns the string representation.

Step 4: Write the DateTime in cell A2.

ws.cell(row=2, column=1).value = time

Step 5: Initiate a sleep of 2 seconds using time.sleep(2)

Step 6: Similarly, get the current datetime again and write it in cell A3. Here you will notice a difference of 2 seconds from the previous DateTime since there was a sleep of 2 seconds in between.

Step 7: Finally, Save the excel workbook with a file name and close the workbook

wb.save('gfg.xlsx')
wb.close()

Below is the full implementation:

Python3




# Import the required modules
import datetime
from openpyxl import Workbook
import time
  
  
# Main Function
if __name__ == '__main__':
    
    # Create a workbook object
    wb = Workbook()
  
    # Select the active sheet
    ws = wb.active
  
    # Heading of Cell A1
    ws.cell(row=1, column=1).value = "Current Date and Time"
  
    # Cell A2 containing the Current Date and Time
    ws.cell(row=2, column=1).value = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
  
    # Sleep of 2 seconds
    time.sleep(2)
  
    # Cell A3 containing the Current Date and Time
    ws.cell(row=3, column=1).value = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
    time.sleep(2)
  
    # Cell A4 containing the Current Date and Time
    ws.cell(row=4, column=1).value = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
  
    # Save the workbook with a
    # filename and close the object
    wb.save('gfg.xlsx')
    wb.close()


Output:



Previous Article
Next Article

Similar Reads

Python | Pandas Timestamp.timestamp
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas Timestamp.timestamp() function returns the time expressed as the number of seconds that have passed since January 1, 1970. That z
3 min read
How to add timestamp to CSV file in Python
Prerequisite: Datetime module In this example, we will learn How to add timestamp to CSV files in Python. We can easily add timestamp to CSV files with the help of datetime module of python. Let's the stepwise implementation for adding timestamp to CSV files in Python. Creating CSV and adding timestampImport csv and datetime module. We will use the
5 min read
Python Script to change name of a file to its timestamp
A Digital Timestamp is a sequence of characters(usually a combination of digits and delimiters), identifying the time when a certain event occurred. In computer science, timestamps are generally used for marking the time of the creation of a virtual entity but are not limited to this in its use case. Digital Timestamps are implemented in various st
3 min read
Create a GUI to convert CSV file into excel file using Python
Prerequisites: Python GUI – tkinter, Read csv using pandas CSV file is a Comma Separated Value file that uses a comma to separate values. It is basically used for exchanging data between different applications. In this, individual rows are separated by a newline. Fields of data in each row are delimited with a comma. Modules Needed Pandas: Python i
3 min read
How to convert PDF file to Excel file using Python?
In this article, we will see how to convert a PDF to Excel or CSV File Using Python. It can be done with various methods, here are we are going to use some methods. Method 1: Using pdftables_api Here will use the pdftables_api Module for converting the PDF file into any other format. It's a simple web-based API, so can be called from any programmin
2 min read
Python | Pandas Timestamp.second
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas Timestamp.second attribute return an integer value which represents the value of second in the given Timestamp object. Syntax : T
2 min read
Python | Pandas Timestamp.date
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas Timestamp.date() function return a datetime object with same year, month and day as that of the given Timestamp object. Syntax :
2 min read
Python | Pandas Timestamp.ctime
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas Timestamp.ctime() function return the time in string format. The format of the returned string object is based on ctime() style.
2 min read
Get UTC timestamp in Python
Datetime module supplies classes to work with date and time. These classes provide a number of functions to deal with dates, times and time intervals. Date and datetime are an object in Python, so when you manipulate them, you are actually manipulating objects and not string or timestamps. Note: For more information, refer to Python datetime module
1 min read
Python | Pandas Timestamp.is_year_start
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas Timestamp.is_year_start attribute return a boolean value. It return True if the date in the given Timestamp object is the start o
2 min read
three90RightbarBannerImg