Can I copy/paste Python code?

Laura Creighton lac at
Tue Jul 21 10:24:19 CEST 2015

In a message of Mon, 20 Jul 2015 19:49:56 -0700, ryguy7272 writes:
>I'm trying to copy some Python code from a PDF book that I'm reading.  I want to test out the code, and I can copy it, but when I paste it into the Shell, everything is all screwed up because of the indentation. Every time I paste in any kind of code, it seems like everything is immediately left-justified, and then nothing works.
>Any idea how to make this work easily?  Without re-typing hundreds of lines of code...
>Thanks to all.

What I do is run my pdf file through pdftotext

and get a text version of the file.  If the file is large, sometimes I
just save the code I want to convert as separate pdf files and then
convert them.  Then I paste from the result, not the pdf.

You want to give the -layout and -nopgbrk options to pdftotext.

It's not perfect, but saves a lot of work.

Make sure your pdf is writing your python code in a fixed width font
before you try this, or it will be hopeless.  If your python code is
being displayed in the book in a variable width font, you will first have
to hack your pdf to stop doing this, and then run it through the tool.

Write back if you need more help.


More information about the Python-list mailing list