How to open a csv file in Microsoft Excel in Python?

12,822

Solution 1

Usually on Windows the .csv filetype is configured to be opened by Excel. In this case you can just do:

from subprocess import Popen
p = Popen('filename.csv', shell=True)

In case it does not work, try pointing the full path of the Excel application:

subprocess.Popen(r'C:\Program Files (x86)\Microsoft Office\Office14\EXCEL.EXE stack.csv')

Solution 2

You can use the 'startfile' command from the os library.

import os
os.startfile('filename.csv')

Do make sure to specify the path of the file or to set the working directory to the path where the file is located.

Share:
12,822

Related videos on Youtube

curiousguy
Author by

curiousguy

Expertise in : Technologies : Python 3.4,2.7,PHP-5,NodeJS,Javascript,REST Api Frameworks: Django 1.8 , Flask-0.12,Cake-PHP, Webview Framework: Angular 2,ReactJS Visualization Framework : D3.js,Highcharts , Amcharts Tool: AWS , Github,Jenkins Database : MYSQL , Postgresql, AWS RDS, Cassandra

Updated on June 04, 2022

Comments

  • curiousguy
    curiousguy almost 2 years
    base_path = os.path.dirname(os.path.abspath(__file__))          
    _csvFilename = os.path.join(base_path, "bcForecasting.csv")
    _csvFile = open (_csvFilename, 'wb')
    _csvFile = csv.writer(_csvFile, quoting=csv.QUOTE_ALL)
    
    _Header = self.makeIntoList (self.root.tss.series () [0].getAllTimes (), self.originalTimesteps + _futurePeriods)
    _csvFile.writerow (_Header)
    

    Now I want to open the created bcForecasting.csv file in Excel. How to do it in Python?

    • DSM
      DSM over 10 years
      I know how to open a csv file in Excel, and how to open a csv file in Python, but what does it mean to open a csv file "in Microsoft Excel in Python"? Do you want Python to tell Excel (starting it up if it isn't running) to open the sheet? [Aside: you didn't close the file. It's usually a better idea to use the with statement.]
    • Paolo
      Paolo over 10 years
    • curiousguy
      curiousguy over 10 years
      Hello DSM , yes actually I "want Python to tell Excel (starting it up if it isn't running) to open the sheet". Actually I am closing the file here I have not provided the full code. Thanks
    • DSM
      DSM over 10 years
      Ah, then the linked question has the answer. The win32com module will work (I've used it myself to script PowerPoint.)
    • curiousguy
      curiousguy over 10 years
      Hello I dont know why it is not opening for me , though I can see in the task manager that EXCEL.exe is running , also I cant delete the file as it is giving an alert "File is now available for editing"
  • Noumenon
    Noumenon over 6 years
    Beware that opening a CSV in Excel may drop leading 0s from numeric fields like zip codes. Import with Data > From Text to avoid this.
  • ZF007
    ZF007 almost 5 years
    As the correct answer was given almost a millennia ago.. you should let the reader know which version of python you refer with your answer and why you should this method instead of the Popen variant.
  • Andy Lynch
    Andy Lynch almost 5 years
    @rajat is right - this has been about since Python 2.0, it uses the Windows shell association to launch the file (has the same effect as double-clicking the file in Windows Explorer), and avoids the overhead/ security concerns of using Popen and cmd in this way (see docs.python.org/2/library/… )