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?

5
  • 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.] Commented Aug 11, 2013 at 16:15
  • 1
    does it help? stackoverflow.com/questions/247724/… Commented Aug 11, 2013 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 Commented Aug 11, 2013 at 16:19
  • Ah, then the linked question has the answer. The win32com module will work (I've used it myself to script PowerPoint.) Commented Aug 11, 2013 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" Commented Aug 11, 2013 at 17:46

3 Answers 3

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')
Sign up to request clarification or add additional context in comments.

1 Comment

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

2 Comments

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.
@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/… )
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.