+{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"name":"Export.ipynb","provenance":[],"collapsed_sections":[]},"kernelspec":{"name":"python3","display_name":"Python 3"}},"cells":[{"cell_type":"markdown","metadata":{"id":"fHPJBytCYnIC","colab_type":"text"},"source":["This file should make exporting any `.ipynb` files to PDF much easier. All you have to do is change the `path` variable to point to the folder that contains the `.ipynb` file, and then put in the file name for the `file_name` variable.\n","\n","Then run all the cells.\n","\n","**REMEMBER: This only exports images which are inserted as `Image(filename=...)`**\n","\n","e.g.\n","```\n","base_path = r'/content/drive/My Drive/' # This part is always the same for a mounted google drive\n","path = 'ENPHYS_259/' # Folder path to the folder that includes your .ipynb file\n","file_name = 'test.ipynb' # .ipynb filename that you want to export to PDF\n","```\n"]},{"cell_type":"code","metadata":{"id":"FX_zyy3WYlCZ","colab_type":"code","colab":{},"executionInfo":{"status":"ok","timestamp":1600206656232,"user_tz":420,"elapsed":548,"user":{"displayName":"Tim Child","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14Gja41a6NZM-sXna8IDZYolHfTSms_knBAFrWLv8=s64","userId":"08949961291514014620"}}},"source":["base_path = r'/content/drive/My Drive/' # This part is always the same for a mounted google drive\n","path = 'ENPHYS_259/Intro/' # Folder path to the folder that includes your .ipynb file\n","file_name = 'Lab_-1_Python_Lab_Notebook.ipynb' # .ipynb filename that you want to export to PDF"],"execution_count":1,"outputs":[]},{"cell_type":"code","metadata":{"id":"GbefQ2omSw73","colab_type":"code","colab":{},"executionInfo":{"status":"ok","timestamp":1600207526296,"user_tz":420,"elapsed":2145,"user":{"displayName":"Tim Child","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14Gja41a6NZM-sXna8IDZYolHfTSms_knBAFrWLv8=s64","userId":"08949961291514014620"}}},"source":["%%capture\n","!apt-get install texlive-xetex texlive-fonts-recommended texlive-generic-recommended pandoc"],"execution_count":2,"outputs":[]},{"cell_type":"code","metadata":{"id":"IvWP_QP_VxNF","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":34},"executionInfo":{"status":"ok","timestamp":1600206756630,"user_tz":420,"elapsed":70119,"user":{"displayName":"Tim Child","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14Gja41a6NZM-sXna8IDZYolHfTSms_knBAFrWLv8=s64","userId":"08949961291514014620"}},"outputId":"f53f59e8-ed34-4aec-80f6-2a58a8e21119"},"source":["# Mount Drive (can use ctrl+d if you have set up the shortcut)\n","from google.colab import drive\n","drive.mount('/content/drive')"],"execution_count":3,"outputs":[{"output_type":"stream","text":["Mounted at /content/drive\n"],"name":"stdout"}]},{"cell_type":"code","metadata":{"id":"EyxIA8nPtjUC","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":71},"executionInfo":{"status":"ok","timestamp":1600206766286,"user_tz":420,"elapsed":586,"user":{"displayName":"Tim Child","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14Gja41a6NZM-sXna8IDZYolHfTSms_knBAFrWLv8=s64","userId":"08949961291514014620"}},"outputId":"728c8c94-136d-4527-d7ce-11ea3a6d8e04"},"source":["import os \n","folder_path = os.path.join(base_path, path)\n","if file_name not in os.listdir(folder_path):\n"," print('WARNING\\nWARNING\\nWARNING\\nFile name not found in folder path!')\n","print(f'Folder Path entered: {folder_path}\\nFiles in folder: {os.listdir(folder_path)}')"],"execution_count":5,"outputs":[{"output_type":"stream","text":["Folder Path entered: /content/drive/My Drive/ENPHYS_259/Intro/\n","Files in folder: ['scatter.xlsx', 'sine1khz.csv', 'sine1khz.txt', 'square1khz.txt', 'Colab Instructions.docx', 'Python_Analysis_Guide.ipynb', 'cat_300.jpg', 'Lab_-1_Python_Lab_Notebook.ipynb', 'Export.ipynb']\n"],"name":"stdout"}]},{"cell_type":"code","metadata":{"id":"ofVNSQK4S94h","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":221},"executionInfo":{"status":"ok","timestamp":1600207012999,"user_tz":420,"elapsed":4150,"user":{"displayName":"Tim Child","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14Gja41a6NZM-sXna8IDZYolHfTSms_knBAFrWLv8=s64","userId":"08949961291514014620"}},"outputId":"66941c97-2c81-41e3-ab04-5e15b42c4847"},"source":["# Capture to prevent lots of output... Remove this if troubleshooting!\n","# %%capture\n","\n","# In case running multiple times, delete old files first\n","pdf_file_name = file_name.split('.')[0]+'.pdf' # Same as file_name with .ipynb changed to .pdf\n","!rm $file_name \n","!rm $pdf_file_name \n","# Dollar signs because references to local variables\n","\n","import os\n","# Copy file to local environment\n","full_path = os.path.join(base_path, path, file_name)\n","!cp \"$full_path\" ./ \n","# Dollar signs because full_path contains a space in 'My Drive' part.\n","\n","# Run nbconvert on local file to produce pdf file\n","!jupyter nbconvert \"$file_name\" --to pdf --template report\n","\n"],"execution_count":7,"outputs":[{"output_type":"stream","text":["[NbConvertApp] Converting notebook Lab_-1_Python_Lab_Notebook.ipynb to pdf\n","[NbConvertApp] Support files will be in Lab_-1_Python_Lab_Notebook_files/\n","[NbConvertApp] Making directory ./Lab_-1_Python_Lab_Notebook_files\n","[NbConvertApp] Making directory ./Lab_-1_Python_Lab_Notebook_files\n","[NbConvertApp] Making directory ./Lab_-1_Python_Lab_Notebook_files\n","[NbConvertApp] Writing 32727 bytes to ./notebook.tex\n","[NbConvertApp] Building PDF\n","[NbConvertApp] Running xelatex 3 times: [u'xelatex', u'./notebook.tex', '-quiet']\n","[NbConvertApp] Running bibtex 1 time: [u'bibtex', u'./notebook']\n","[NbConvertApp] WARNING | bibtex had problems, most likely because there were no citations\n","[NbConvertApp] PDF successfully created\n","[NbConvertApp] Writing 114573 bytes to Lab_-1_Python_Lab_Notebook.pdf\n"],"name":"stdout"}]},{"cell_type":"markdown","metadata":{"id":"QzWezpVpWNFw","colab_type":"text"},"source":["Now refresh the local files on the left, the `.pdf` file should be there to download.\n","\n","**If it isn't working:**\n","1. Check your folder_path and file_name are correct\n","2. Remove `%%capture` from the cell above and look at the top few lines of output\n"]}]}
0 commit comments