Closing an Excel File Using Python Last Updated : 20 Mar, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report We are given an excel file that is opened and our task is to close that excel file using different approaches in Python. In this article, we will explore three different approaches to Closing Excel in Python. Closing an Excel Session with PythonBelow are the possible approaches to Using Os In Python For Closing Excel: Using win32comUsing os.system with taskkill.Using psutil.Note: The Excel application should be opened before performing all the below executions. Closing Excel in Python Using win32comIn this example, we are using the win32com library in Python to interact with Microsoft Excel. The code creates an instance of the Excel application using the Dispatch function and subsequently closes the Excel application by invoking the Quit() method. This allows for programmatic control and closure of Excel through Python. Python3 import win32com.client excel = win32com.client.Dispatch("Excel.Application") excel.Quit() print("Excel closed using win32com") Output: Closing Excel Using os.system with taskkillIn this example, we are using the os.system function in Python to execute a command-line instruction to forcefully terminate the Excel process. The command taskkill /f /im excel.exe kills all instances of Excel (excel.exe) forcefully (/f). The print statement indicates that Excel has been terminated using the taskkill command. Python3 import os os.system("taskkill /f /im excel.exe") print("Excel terminated using taskkill") Output: Closing Excel Using psutilIn this example, we are using the psutil module in Python to iterate through the running processes. When a process with the name "EXCEL.EXE" is found, the kill() method is called to terminate the Excel process. The print statement indicates that Excel has been closed using the psutil module. Python3 import psutil for proc in psutil.process_iter(): if proc.name() == "EXCEL.EXE": proc.kill() print("Excel closed using psutil") Output: Comment More infoAdvertise with us Next Article Exporting Multiple Sheets As Csv Using Python V vishnuvardhan1510 Follow Improve Article Tags : Python Python Programs python-os-module Python-excel Practice Tags : python Similar Reads Copy Rows and Columns in Excel Using Python Manipulating data in Excel often involves tasks such as copying specific rows or columns based on certain conditions. Python, with its powerful libraries like Pandas and openpyxl, provides efficient solutions for handling Excel files. In this article, we will explore how to copy rows and columns in 3 min read Close a File in Python In Python, a file object (often denoted as fp) is a representation of an open file. When working with files, it is essential to close the file properly to release system resources and ensure data integrity. Closing a file is crucial to avoid potential issues like data corruption and resource leaks. 2 min read How to Close MP3 Files Using Python? Manipulating audio files, such as MP3s, is a common task in Python programming, especially in applications involving audio processing, music players, or audio analysis. This article will explore concepts related to this topic and guide how to effectively close MP3 files in Python. Closing MP3 files 2 min read How to Append Data in Excel Using Python We are given an Excel file and our task is to append the data into this excel file using Python. In this article, we'll explore different approaches to append data to an Excel file using Python. Append Data in Excel Using PythonBelow, are some examples to understand how to append data in excel using 2 min read Exporting Multiple Sheets As Csv Using Python In data processing and analysis, spreadsheets are a common format for storing and manipulating data. However, when working with large datasets or conducting complex analyses, it's often necessary to export data from multiple sheets into a more versatile format. CSV (Comma-Separated Values) files are 3 min read Python Delete File When any large program is created, usually there are small files that we need to create to store some data that is needed for the large programs. when our program is completed, so we need to delete them. In this article, we will see how to delete a file in Python. Methods to Delete a File in Python 4 min read Like