Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opening the Excel application from Python

I am using 'xlwt' to write into Excel files as part of my project in Python. I also need to actually open the Excel spreadsheet for display and also close it. I found a function:

import webbrowser
webbrowser.open('C:/Users/300231823/Desktop/GUI/simplenew4.xls')

This seems to open the .xls file. How do I close the file?

I am completely new to programming, and I started using Python 3 weeks ago.

like image 746
Sriram Samynathan Avatar asked Jun 08 '11 16:06

Sriram Samynathan


People also ask

How do I automatically open an Excel file in Python?

To work on this Excel sheet we are going to use a library openpyxl. Create a folder in your directory, give it a name and install the openpyxl package by executing the following command in your terminal. Now we can import this package to work on our spreadsheet. Before that add the spreadsheet in your project folder.

Can I program Excel with Python?

Python for Excel. and Google Sheets. xlwings is an open-core spreadsheet automation package with a beautiful API. It's a sane alternative to VBA macros/UDFs and Power Query and works across Excel on Windows and macOS, Excel on the Web, and Google Sheets.

How do I integrate Excel into Python?

PyXLL is very different to these other packages. Instead of just allowing you to read and write Excel files, PyXLL integrates Python into Excel. This allows you to run Python inside of Excel to extend Excel's capabilities with your own Python code!


1 Answers

from win32com.client import Dispatch

xl = Dispatch("Excel.Application")
xl.Visible = True # otherwise excel is hidden

# newest excel does not accept forward slash in path
wb = xl.Workbooks.Open(r'C:\Users\300231823\Desktop\GUI\simplenew4.xls')
wb.Close()
xl.Quit()

The win32com module is part of pywin32.

like image 112
Steven Rumbalski Avatar answered Oct 16 '22 13:10

Steven Rumbalski