6

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.

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

papafe
  • 2,959
  • 4
  • 41
  • 72

3 Answers3

15

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

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

Here, enscript does 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 ps2pdf which is from the ghostscript package.

Installation

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

apt-get install enscript

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

apt-get install ghostscript
John1024
  • 109,961
  • 14
  • 137
  • 171
8

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

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

You will get abc.ps with syntax highlight, then

ps2pdf abc.ps abc.pdf
PasteBT
  • 2,128
  • 16
  • 17
0

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

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

Read this SO link for more details.

For pdf-printing part, what I normally do is create an html template of my text/document and then use the xhtml2pdf python library to generate the 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 article to understand more about this method of gerating pdfs in python.

Community
  • 1
  • 1
Prahlad Yeri
  • 3,567
  • 4
  • 25
  • 55