Skip to main content

Convert Your Colab Notebook to a PDF. One-Minute Install. Zero Configuration.

Project description

Directions: Copy→Paste→Run Open In Colab

# Colab2PDF v1.0.0 by Drengskapur (github.com/drengskapur/colab2pdf)
# License: GPL-3.0-or-later
# @title {display-mode:"form"}
# @markdown ⬇️ Download PDF
def colab2pdf():
    ENABLE=True # @param {type:"boolean"}
    if ENABLE:
        import os, datetime, json, pathlib, urllib, requests, werkzeug, nbformat, google.colab, yaml, warnings
        NAME = pathlib.Path(werkzeug.utils.secure_filename(urllib.parse.unquote(requests.get(f"http://{os.environ['COLAB_JUPYTER_IP']}:{os.environ['KMP_TARGET_PORT']}/api/sessions").json()[0]["name"])))
        TEMP = pathlib.Path("/content/pdfs") / f"{datetime.datetime.now().strftime('%Y%m%d_%H%M%S')}_{NAME.stem}"; TEMP.mkdir(parents=True, exist_ok=True)
        NB = [cell for cell in nbformat.reads(json.dumps(google.colab._message.blocking_request("get_ipynb", timeout_sec=30)["ipynb"]), as_version=4).cells if "--Colab2PDF" not in cell.source]
        warnings.filterwarnings('ignore', category=nbformat.validator.MissingIDFieldWarning)
        with (TEMP / f"{NAME.stem}.ipynb").open("w") as nb_copy: nbformat.write(nbformat.v4.new_notebook(cells=NB or [nbformat.v4.new_code_cell("#")]), nb_copy)
        if not pathlib.Path("/usr/local/bin/quarto").exists():
            !wget -q "https://quarto.org/download/latest/quarto-linux-amd64.deb" -P {TEMP} && dpkg -i {TEMP}/quarto-linux-amd64.deb > /dev/null && quarto install tinytex --update-path --quiet
        with (TEMP / "config.yml").open("w") as file: yaml.dump({'include-in-header': [{"text": r"\usepackage{fvextra}\DefineVerbatimEnvironment{Highlighting}{Verbatim}{breaksymbolleft={},showspaces=false,showtabs=false,breaklines,breakanywhere,commandchars=\\\{\}}"}],'include-before-body': [{"text": r"\DefineVerbatimEnvironment{verbatim}{Verbatim}{breaksymbolleft={},showspaces=false,showtabs=false,breaklines}"}]}, file)
        !quarto render {TEMP}/{NAME.stem}.ipynb --metadata-file={TEMP}/config.yml --to pdf -M latex-auto-install -M margin-top=1in -M margin-bottom=1in -M margin-left=1in -M margin-right=1in --quiet
        google.colab.files.download(str(TEMP / f"{NAME.stem}.pdf"))
colab2pdf()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

colab2pdf-1.0.0a9.tar.gz (81.0 kB view details)

Uploaded Source

Built Distribution

colab2pdf-1.0.0a9-py3-none-any.whl (19.1 kB view details)

Uploaded Python 3

File details

Details for the file colab2pdf-1.0.0a9.tar.gz.

File metadata

  • Download URL: colab2pdf-1.0.0a9.tar.gz
  • Upload date:
  • Size: 81.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.6

File hashes

Hashes for colab2pdf-1.0.0a9.tar.gz
Algorithm Hash digest
SHA256 6aa0a722dab8232cf0cb44c7b2558c511ac2b3a4cad2d8c01417bec91eed6fab
MD5 d2b47c1393f9be0cdfa970fff607f5c0
BLAKE2b-256 3b97cdc5fc7e55730c36d39ba61fecd70ec153adb671195ea9fe275791dfdd2c

See more details on using hashes here.

File details

Details for the file colab2pdf-1.0.0a9-py3-none-any.whl.

File metadata

  • Download URL: colab2pdf-1.0.0a9-py3-none-any.whl
  • Upload date:
  • Size: 19.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.6

File hashes

Hashes for colab2pdf-1.0.0a9-py3-none-any.whl
Algorithm Hash digest
SHA256 4a815bc9d8cf8ccbd639cb57f3f5579bd9a0de1d8f34aa662a3ef30d13fcca69
MD5 b1cd274b93c55d777d4affa2c9c654b2
BLAKE2b-256 d440b6bde9628aecc83968b924bb36b6f4b8697362c88219866be0effdfd4a6f

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page