2

I'm trying to open an Excel file from Python. The code is:

import win32com.client
excelApp = win32com.client.Dispatch('Excel.Application')
book = excelApp.Open(os. getcwd() + '\Output ' + get_date() + '.csv')

but I'm getting this error: "AttributeError: Excel.Application.Open". Any idea how to fix it? Thanks!

6
  • 1
    Why do you think this attribute should exist? Commented Dec 24, 2019 at 19:32
  • why just not use pandas pandas.pydata.org/pandas-docs/stable/reference/api/… ? Commented Dec 24, 2019 at 20:07
  • @GiovaniSalazar I'm not trying to read it into pandas or python. I'm trying to open the Microsoft Excel application itself. Commented Dec 24, 2019 at 20:13
  • 1
    try this: book = excelApp.Workbooks.Open(os. getcwd() + '\Output ' + get_date() + '.csv') Commented Dec 24, 2019 at 20:21
  • 1
    @RuslanS. Perfect!...that worked but the Excel app was hidden so I had to use excelApp.Visible = True to see it. Thanks! :-) Commented Dec 24, 2019 at 20:28

2 Answers 2

2

This code worked as per @RuslnS. 's recommendation:

import os
import win32com.client
excelApp = win32com.client.Dispatch('Excel.Application')
excelApp.Workbooks.Open(os.getcwd() + '\Groups.csv')
excelApp.Visible = True

This too works as per Jack Taylor's recommendation:

import subprocess
subprocess.Popen(["C:\\Program Files\\Microsoft Office\\Root\\Office16\\EXCEL.EXE", "/t", 'D:\\test.xlsx'])
Sign up to request clarification or add additional context in comments.

Comments

1

According to the Microsoft Office docs, all Office programs support a /t command-line switch to open a specific file. You can use this from Python's subprocess module to open a specific file in Excel.

import subprocess

subprocess.Popen(["excel.exe", "/t", "C:\\Path\\to\\my\\spreadsheet.xlsx"])

3 Comments

Thanks. I've tried subprocess.Popen(["excel.exe", "/t", 'D:\\test.xlsx']) it but I'm getting [WinError 2] The system cannot find the file specified even though I'm sure hat the file exists. Did you manage to get it working?
You might need to specify the full path to excel.exe, depending on how your PATH environment variable is set up.
Yup..that worked, thanks! :-) subprocess.Popen(["C:\\Program Files\\Microsoft Office\\Root\\Office16\\EXCEL.EXE", "/t", 'D:\\test.xlsx'])

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.