Search code examples
pythonjupyter-notebookpdf-generationjupyter

How to programatically save pdf of jupyter notebook with a custom, dynamic, file name


On Windows 10, I am trying to save a Jupyter notebook as a pdf, under a name that will change for every run of the notebook.

Here is what I have so far:

name1 = 'July'
name2 = 'August'
jupyter_nb_filename = '{}_vs_{}'.format(name1,name2)
!jupyter nbconvert --output-dir="C:\\mydir\\" --output=jupyter_nb_filename --to pdf --TemplateExporter.exclude_input=True mynotebook.ipynb

  • Current behaviour: Jupyter notebook is stored as "jupyter_nb_filename.pdf"
  • Desired behaviour. Jupyter notebook to be stored as "July_vs_August.pdf"

I looked in the documentation but could not figure it out.

Any help would be greatly appreciated.


Solution

  • You have to prefix the variable name with $ for it to be interpreted as a variable in a console command. See this question.

    !jupyter nbconvert --output-dir="C:\\mydir\\" --output=$jupyter_nb_filename --to pdf --TemplateExporter.exclude_input=True mynotebook.ipynb