Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python PDF>JPG conversion with pdf2jpg

I'm trying convert some PDFs into JPG, and using pdf2jpg to do this.

The code I run is:

inputPath = sys.argv[1].replace("\\", "/")
print(inputPath)

# Get parent folder of the file
parentFolder = "/".join(inputPath.split("/")[:-1])
print(parentFolder)

# Convert pdf to jpg in same folder
result = pdf2jpg.convert_pdf2jpg(inputPath, parentFolder, pages="1")
print(result)

When I run this, the error I get is:

NotADirectoryError: [WinError 267] The directory name is invalid: 'C:/Users/Username/Desktop\\test.pdf'

The weird thing is, when I run the same code with the last 2 lines commented out, I get:

C:/Users/Username/Desktop/test.pdf
C:/Users/Username/Desktop

It seems like the inputPath itself is being converted to forward slashes correctly, but then being reverted to backslashes when being referenced by pdf2jpg.

ETA: Switched to backslashes instead of forward slashes, and using raw literals. Code now as below:

inputPath = sys.argv[1]
inputPath_raw = r'%s'%inputPath
print(inputPath_raw)

parentFolder = chr(92).join(inputPath_raw.split(chr(92))[:-1])
print(parentFolder)

result = pdf2jpg.convert_pdf2jpg(inputPath_raw, parentFolder, pages="1")
print(result)

Then I give it the input:

convert.py "C:\Users\Username\Desktop\test.pdf"

And the error I see is:

NotADirectoryError: [WinError 267] The directory name is invalid: 'C:\\Users\\Username\\Desktop\\test.pdf'

Printing the variables still gives the correct output as the file paths, it's just this part which fails to recognise the path.

like image 750
CrowStorm Avatar asked Dec 07 '25 10:12

CrowStorm


1 Answers

UPDATE:

The problem is that pdf2jpg is trying to create a directory, named after your pdf file in the outputpath. When converting the file test.pdf, it will (try to) create a directory named \test.pdf in the specified outputpath.

Since the source and destination directories are identical it will fail, because it's a filesystem limitation to have a file and a directory with the same name test.pdf in the same path.

Here is a test to create the output jpgs in inputpath + \pdf2jpg dir and it will work:

# -*- coding: utf-8 -*-    
import os
import sys
from pdf2jpg import pdf2jpg

source = sys.argv[1]
destination = os.path.dirname(source)+"\pdf2jpg"

try:
    os.mkdir(destination)
except FileExistsError:
    # pdf2jpg directory existing
    pass

result = pdf2jpg.convert_pdf2jpg(source, destination, pages="ALL")
like image 130
Michael D. Avatar answered Dec 10 '25 01:12

Michael D.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!