Python PDF JPG conversion with pdf2jpg

Python PDF >JPG conversion with pdf2jpg … here is a solution to the problem.

Python PDF >JPG conversion with pdf2jpg

I’m trying to convert some PDF to JPG and use pdf2jpg to do this.

The code I ran 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 it, the error I get is:

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

Oddly enough, when I run the same code with the last two lines commented out, I get:

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

It seems that the inputPath itself is correctly converted to a forward slash, but reverts to a backslash when referenced by pdf2jpg.

ETA: Switch to a backslash instead of a forward slash and use the original text. Now the code is as follows:

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 type it :

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

The error I see is:

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

The print variable still gives the correct output as the file path, but this part does not recognize the path.

Solution

Update:

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

It fails because the source and destination directories are the same

because files and directories with the same name test.pdf in the same path are file system restrictions.

This is a test to create an output jpg in the inputpath + \pdf2jpg directory, 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")

Related Problems and Solutions