#!/usr/bin/env python3 """ Current version of Jupyter doesn't support pdf exporting when it comes to russian language in the document. To fix this, current script has born. It requires nbconvert as long as jupyter to be installed. Author: Sergey Ivanychev Email: sergeyivanychev@gmail.com Revision: 1 """
import sys import os import subprocess
FORMAT = ".ipynb" """ TODO: What if user has ___o.tex in current folder? """ TEMP_TEX = "___o.tex" TEMP_FOLDER = "/tmp" TRASH_EXTENSIONS = [".aux", ".out", ".log"] OLD1 = r"\usepackage[utf8x]{inputenc}" NEW1 = r""" \usepackage[utf8x]{inputenc} \usepackage{ctex} """ REPLACE = [(OLD1, NEW1)] HELP = """ This script is aimed at correctly converting .ipynb to .pdf files. You may use it via # ipynb2pdf Solution.ipynb To get .pdf in the same directory """
defcheck_args(argv): if (len(argv) > 2): print("Please, type .ipynb filename as argument") return-1 if (len(argv) == 1): print(HELP) return-1 script, filename = argv ifnot is_ipynb(filename): print("Please, type .ipynb filename as argument") return-1
defremove_extention(string, extention): """ Removes argumented extention from the end of the string """ if extention[0] != '.': extention = "." + extention if string.endswith(extention): string = string[0:-len(extention)] return string
deftex2pdf(filename, desired_name): """ Converts filename (which is tex document) to desired_name (which is pdf) leaving no trash in current folder """ base = remove_extention(filename, "tex") output = base + ".pdf" ret = subprocess.call("xelatex %s" % filename , shell=True, stdout=subprocess.PIPE) for ext in TRASH_EXTENSIONS: os.unlink(base + ext) os.rename(output, desired_name) return ret