Simple Python script to convert jupter notebook IPYNB file to PDF (contains Chinese)

Source: Internet
Author: User

Directly executed Python code ipynb2pdf.py
  
 
  1. # coding:utf-8
  2. import sys
  3. import os
  4. import re
  5. import shutil
  6. notebook = sys.argv[1]
  7. texFile = notebook.replace(‘.ipynb‘,‘.tex‘)
  8. # 1.convert .ipynb to latex file .tex
  9. # 将ipynb文件转为tex文件
  10. print ‘1. convert ‘ + notebook + ‘ to ‘ + texFile
  11. print ‘------ \n‘
  12. os.system(r‘jupyter nbconvert --to latex ‘ + notebook)
  13. print ‘convert over‘
  14. # 2. add Chinese support by adding the string below
  15. # 加入引用的包使支持中文(直接转换中文会丢失)
  16. # \usepackage{fontspec, xunicode, xltxtra}
  17. # \setmainfont{Microsoft YaHei}
  18. # \usepackage{ctex}
  19. print ‘2. add Chinese support to .tex file‘
  20. print ‘------‘
  21. file = open(texFile,‘r‘)
  22. str_file = file.read()
  23. strinfo = re.compile(‘(documentclass[\d\D]+\{article\})‘) #查找的字符line0
  24. m=re.findall(strinfo,str_file)
  25. if len(m) == 0:
  26. print r‘can not find documentclass[**pt]{article}‘
  27. sys.exit(1)
  28. str_file = strinfo.sub(‘\\1 \n \\usepackage{fontspec, xunicode, xltxtra} \n \\setmainfont{Microsoft YaHei} \r \\usepackage{ctex}‘,str_file) # 替换的字符line1
  29. file.close()
  30. file = open(texFile,‘w‘)
  31. file.write(str_file)
  32. file.close()
  33. print ‘add Chinese support successed‘
  34. # 3. convert .tex to .pdf by xelatex
  35. # 使用xelatex命令编译.tex文件得到pdf
  36. print ‘3. convert tex to pdf‘
  37. print ‘------‘
  38. os.system(‘xelatex ‘ + texFile)
  39. print ‘convert pdf successed‘
  40. # 4. delete the auxiliary files
  41. # 清理生成的中间文件
  42. # change there if latex file is needed
  43. print ‘4. delete auxiliary files‘
  44. print ‘------‘
  45. os.remove(notebook.replace(‘.ipynb‘,‘.aux‘))
  46. os.remove(notebook.replace(‘.ipynb‘,‘.log‘))
  47. os.remove(notebook.replace(‘.ipynb‘,‘.out‘))
  48. # change there if latex file is needed
  49. os.remove(notebook.replace(‘.ipynb‘,‘.tex‘))
  50. if os.path.isdir(notebook.replace(‘.ipynb‘,‘_files‘)):
  51. shutil.rmtree(notebook.replace(‘.ipynb‘,‘_files‘))
  52. print ‘delete auxiliary files successed‘
Invocation mode
    1. Open the command line in the current directory (ipynb2pdf.py and. ipynb files in the directory)
    2. Input command
      python ipynb2pdf.py yourFilename.ipynb
Package as EXE (can be ignored)

Use Pyinstaller to package as exe as command-line command
pyinstaller -F ipynb2pdf.py
The generated EXE is placed under the folder in the system's path directory and can be processed directly from the command.

When called, open the command line under the folder where the. Ipynb is located, enter the command
ipynb2pdf yourFilename.ipynb



Null

Simple Python script to convert jupter notebook IPYNB file to PDF (contains Chinese)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.