Print python code in PDF

I need to print a discrete amount of python script to a PDF. The code should be included as text, not an image, since it should be checked by the anti-plagiarism mechanism (I don’t need to take care of this, it just requires that the PDF file is not made from the image, so that’s all). It is also necessary to have syntax highlighting.

Do you have any suggestions for a specific tool for this?

+4
source share
3 answers

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

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

enscript . postscript pdf. ps2pdf, ghostscript.

Debian Unbuntu enscript

apt-get install enscript

Debian Unbuntu ps2pdf ghostscript, , , . . :

apt-get install ghostscript
+12

vim, :

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

abc.ps ,

ps2pdf abc.ps abc.pdf
+5

- urllib2 Google, , :

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

SO.

pdf- , , html- /, xhtml2pdf python 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)

In the html template, you can use tags <code>or <pre>to format your scripts. Read my full article to learn more about this method of transferring pdf files in python.

0
source

All Articles