i want to convert my pdf files with ImageMagic python module PythonMagick but i havent found any solution on net.

import PythonMagick
im = PythonMagick.Image('files.pdf')
im.write("file_img%d.png")

i am using below code and it returns only first page of my pdf file.

any help would be greatly appreciated

since there really isnt a whole lot of documentation (at least that I could find) I will make an assumption --
PDF files are basically a collection of "pages" and it seems that you are simply running the create image function on the first "page"

I would encourage you to find a way to break apart the pages of the pdf, and run the image creation individually....

you can probably try...

from pyPdf import PdfFileReader
from PythonMagick import Image


myfile = PdfFileReader('files.pdf')
pages = myfile.getNumPages()

for page in pages:
    im = Image(myfile.getPage(i+1))
    im.write('file_image{}.png'.format(i+1))

Now.. Im a bit of a newb with Python... so.. would be interested to know if I figured that out right.. You may have to download the PyPdf package to make it work, though...

Ryan

Here is how I split a pdf file into pages from python by calling pdftk

pdf_name = "foo.pdf"
ndigits = 2
com_line = "pdftk A={src} burst output {dst}".format(
        src = pdf_name,
        dst = "%0{ndigits}d.pdf".format(ndigits=ndigits))
)
subprocess.call(com_line, shell=True)

Edited 4 Years Ago by Gribouillis

cant figure out how to edit my post... so...
I made a boo boo..

change 'i+1' to 'page+1' in both instances, assuming my script works in the first place...

and the more I look at it, I think pages would be an int... so the for line should read:

for page in range(pages):
    ....

Edited 4 Years Ago by ryantroop: bad coding :( Sorry

I'm not a developer, i always use this <snip> to convert pdf to image online.

Edited 1 Year Ago by Gribouillis: irrelevant and spammish

Comments
irrelevant and spammish
This article has been dead for over six months. Start a new discussion instead.