将python代码打印为PDF

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20412038/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 20:22:22  来源:igfitidea点击:

Printing python code to PDF

pythonpdfprinting

提问by papafe

I have to print to PDF a discrete amount of python script. The code should be included as text, rather than image, as it must be checked by an anti-plagiarism mechanism (I do not have to take care of this, it is just requested that the pdf is not made from an image, that's all). It is also necessary to have syntax highlighting.

我必须将离散数量的 python 脚本打印为 PDF。代码应该作为文本而不是图像包含在内,因为它必须通过反抄袭机制进行检查(我不必处理这个,只是要求pdf不是由图像制成的,仅此而已)。还需要语法高亮。

Do you have any suggestion regarding a specific tool for this?

你对这个特定的工具有什么建议吗?

采纳答案by John1024

For the anti-plagiarism part, I can't help you. For printing a py script with syntax highlighting, a traditional tool is enscript:

对于反抄袭的部分,我帮不了你。要打印带有语法高亮显示的 py 脚本,传统的工具是enscript

enscript -E -q -Z -p - -f Courier10 readmaya.py | ps2pdf - out.pdf

Here, enscriptdoes the syntax highlighting and produces postscript. You could use any of several possible tools for the second step of converting postscript to pdf. I have shown here ps2pdfwhich is from the ghostscript package.

在这里,enscript进行语法突出显示并生成后记。在将 postscript 转换为 pdf 的第二步中,您可以使用多种可能的工具中的任何一种。我在这里展示了ps2pdf来自 ghostscript 包的内容。

Installation

安装

On a Debian- or Unbuntu-like system, enscriptcan be installed with:

在类似 Debian 或 Unbuntu 的系统上,enscript可以安装:

apt-get install enscript

On a Debian- or Unbuntu-like system, ps2pdfis part of the ghostscriptpackage which is likely already installed. If it isn't. run:

在类似 Debian 或 Unbuntu 的系统上,它是可能已经安装ps2pdfghostscript软件包的一部分。如果不是。跑:

apt-get install ghostscript

回答by PasteBT

If you are using vim, then you can do this:

如果您使用的是 vim,那么您可以这样做:

vim abc.py -c ":hardcopy > abc.ps" -c ":q"

You will get abc.ps with syntax highlight, then

您将获得带有语法高亮显示的 abc.ps,然后

ps2pdf abc.ps abc.pdf

回答by Prahlad Yeri

For anti-plagiarism check, you can use urllib2 to do a google search including a quoted text of what you want to check:

对于反抄袭检查,您可以使用 urllib2 进行谷歌搜索,包括您要检查的引用文本:

import urllib2
response = urllib2.urlopen('https://www.google.co.in/search?q="python+curl"')
html = response.read()

Read thisSO link for more details.

阅读SO 链接以获取更多详细信息。

For pdf-printing part, what I normally do is create an html template of my text/document and then use the xhtml2pdfpython library to generate the pdf:

对于pdf打印部分,我通常做的是为我的文本/文档创建一个html模板,然后使用xhtml2pdfpython库生成pdf:

f=open('template.htm','r')
sourceHtml = unicode(f.read(), errors='ignore')
f.close()
sourceHtml = template.render(tvals)
sourceHtml = sourceHtml.replace('__name__',sname)
sourceHtml = sourceHtml.replace('__address__',saddress)
sourceHtml = sourceHtml.replace('__occupation__',soccupation)
packet = StringIO.StringIO() #write to memory
pisa.CreatePDF(sourceHtml,dest=packet)

Within the html template, you can use <code>or <pre>tags to format your scripts. Read my complete articleto understand more about this method of gerating pdfs in python.

在 html 模板中,您可以使用<code><pre>标签来格式化脚本。阅读我的完整文章以了解有关这种在 python 中生成 pdf 的方法的更多信息。