Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Unable to reference a excel marco file while running a juypter-notebook from another notebook

I’m trying to run a jupyter notebook file called Seg_pivot.ipynb from main.ipynb. The main.ipynb has one line of code: %run "fi/Seg_pivot.ipynb"

The current directory structure looks like the following: enter image description here

The Seg_pivot.ipynb runs a module in the MyMacro, used to style the files in the result folder. When I run the Seg_pivot.ipynb everything works. But when I run the main.ipynb, it gives a FileNotFound error: No such file: ‘mymacro.xlsm’

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

In the Seg_pivot.ipynb this is how I call the macro:

import xlwings as xw
wb = xw.Book("MyMacro.xlsm")
s_macro = wb.macro("Module2.styleF")
s_macro()

>Solution :

I think putting inside your notebook Seg_pivot.ipynb near the top, the following may help:

import pathlib
%cd {pathlib.Path(__file__).parent.resolve()}

That will always set the working directory to where Seg_pivot.ipynb is in your file hierarchy when it runs.

Getting he path to the notebook file something is running it is based on here. The magic %cd command is documented here.

In your case, you may wish to reset the current working directory back in main.ipynb, too. After it runs the Seg_pivot.ipynb notebook if main.ipynb involves any paths after that.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading