0

With excel already open, at the end of some code I am simply trying to open 2 excel files using the following code. However, nothing loads!

from urllib.request import urlopen
from bs4 import BeautifulSoup
import csv
import datetime
import openpyxl
import time

openPythonQuoteLS = openpyxl.load_workbook('c:\ls_equity\quote\PythonQuoteLS.xlsx')

openQuoteLS = openpyxl.load_workbook('c:\ls_equity\quote\QuoteLS.xlsm')
Merc1203
  • 13
  • 2
  • What do you mean "nothing loads"? What does that mean? Are you saying that `openpyxl.load_workbook(...)` returns nothing? – Aran-Fey Mar 24 '18 at 19:40
  • What I mean is that I am trying to open the workbooks - but neither opens up in excel ... – Merc1203 Mar 24 '18 at 19:43
  • ...Of course they don't, that's not what openpyxl does. Take a look at [this question](https://stackoverflow.com/questions/13720940/opening-running-excel-file-from-python). – Aran-Fey Mar 24 '18 at 19:46
  • Doesn't illuminate - the excel application is already open, several workbooks open in it. I - simply - need to open other workbooks. So if openpyxl doesn't do that, what do I need to import to open up an additional excel file? – Merc1203 Mar 24 '18 at 19:50
  • In excel VBA, I would write this to open amzn.xlsm ... Sub OpenAMZN() Workbooks.Open Filename:="C:\LS_Equity\Bushido\amzn.xlsm", UpdateLinks:=3 End Sub – Merc1203 Mar 24 '18 at 19:55
  • All I am asking is ... what is the Python equivalent? – Merc1203 Mar 24 '18 at 19:55
  • There probably isn't an exact equivalent. You have to start an Excel process and tell it to open the xls file, as shown in the question I linked earlier. – Aran-Fey Mar 24 '18 at 19:57
  • I'm afraid I'm not explaining myself clearly enough. Imagine the excel application is open, with a number of files already open. I simply want to open an additional existing file. What is the Python code to do that? Many thanks. – Merc1203 Mar 24 '18 at 20:25
  • 1
    I don't know of a way to open the file in an already opened Excel process. Opening it in a new process is likely the best you can do. – Aran-Fey Mar 24 '18 at 20:26

1 Answers1

0

If all you want to do is open files in Excel, you can just use the OS library to send the command to the OS to open the file, as Aran-Fey mentioned above.

For your specific files:

import os
# ...
os.system('start excel.exe c:\ls_equity\quote\PythonQuoteLS.xlsx')
os.system('start excel.exe c:\ls_equity\quote\QuoteLS.xlsm')
Eliot K
  • 614
  • 5
  • 16
  • Many thanks Eliot, but the excel application will already be open with a file that needs to read-in data from 'QuoteLS.xlsm', so I don't need to open another instance of the excel application, simply a file in an already open excel application – Merc1203 Mar 25 '18 at 07:12