7
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?

Paolo
  • 20,112
  • 21
  • 72
  • 113
curiousguy
  • 3,212
  • 8
  • 39
  • 71
  • 1
    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.] – DSM Aug 11 '13 at 16:15
  • 1
    does it help? http://stackoverflow.com/questions/247724/how-can-i-launch-an-instance-of-an-application-using-python – Paolo Aug 11 '13 at 16:17
  • 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 – curiousguy Aug 11 '13 at 16:19
  • Ah, then the linked question has the answer. The `win32com` module will work (I've used it myself to script PowerPoint.) – DSM Aug 11 '13 at 16:22
  • 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" – curiousguy Aug 11 '13 at 17:46

3 Answers3

8

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')
Saullo G. P. Castro
  • 56,802
  • 26
  • 179
  • 234
  • 2
    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. – Noumenon Sep 06 '17 at 23:47
3

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.

rajat
  • 31
  • 1
  • 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. – ZF007 Apr 25 '19 at 23:55
  • @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 https://docs.python.org/2/library/subprocess.html#frequently-used-arguments ) – Andy Lynch Jul 17 '19 at 11:03
0

On a mac, use the following:

import os
os.system('open filename.csv')

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

tomglynch
  • 1,566
  • 1
  • 10
  • 15