python--jupyter notebook 转化为PDF教程
生活随笔
收集整理的這篇文章主要介紹了
python--jupyter notebook 转化为PDF教程
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
安裝
需要安裝pdfkit:pip install pdfkit
wkhtmltopdf的下載地址:https://www.qqtn.com/down/315109.html 【普通下載】
安裝之后將目錄文件放在環境變量中:C:\Program Files\wkhtmltopdf\bin(查看自己的安裝目錄)
使用
然后在python文件中粘貼如下代碼:
# 執行命令行的包 也可以用os.system import subprocessimport pdfkit# 獲得ipynb文件 inputfile = r'E:\python\yl_site\youlan_project\jupyter_project\test_pdf.ipynb' # 截取ipynb前面的名字,并保留一份html臨時文件 # 這份文件會在轉換過程中需要 # 因為我是利用jupyter對于html的支持,使用pdfkit對html文件進行轉換 temp_html = inputfile[0:inputfile.rfind('.')]+'.html' # 轉換ipynb文件為html # 調用了ipython接口 command = 'ipython nbconvert --to html ' + inputfile # shell端執行command subprocess.call(command, shell=True) print('============success===========') # 拼接一個pdf名字 output_file = inputfile[0:inputfile.rfind('.')]+'.pdf' # 大殺器出場,pdfkit直接將html轉換成pdf path_wk = r'C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe' #安裝位置 config = pdfkit.configuration(wkhtmltopdf = path_wk) pdfkit.from_file(temp_html, output_file, configuration=config) # pdfkit.from_string("hello world","1.pdf",configuration=config)#字符轉PDF # 刪除html臨時文件 subprocess.call('rm '+temp_html, shell=True)搞定。。。
有什么問題請留言,如果覺得有用請點贊支持下。
總結
以上是生活随笔為你收集整理的python--jupyter notebook 转化为PDF教程的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JedisConnectionExcep
- 下一篇: 视频里去水印