Skip to content

Instantly share code, notes, and snippets.

@PandaSekh
Last active November 14, 2020 17:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PandaSekh/657a81437a49654a4ae6b59b4a0ad503 to your computer and use it in GitHub Desktop.
Save PandaSekh/657a81437a49654a4ae6b59b4a0ad503 to your computer and use it in GitHub Desktop.
Convert PDFs to CBR.
# Simple python script to bulk convert PDFs to CBR. Download the .py file and copy it into a folder with the PDFs you want to convert.
# The script won't look for PDFs into subdirectories, but only into the current dir. Open the terminal and run the script.
# No configuration needed. The script will also convert all CBZ to CBR for consistency.
# ImageMagick needs to be installed.
import fnmatch
import os
import shutil
import subprocess
import sys
files = []
filename = ""
x = 0
def getFiles():
for file in os.listdir('.'):
if fnmatch.fnmatch(file, '*.pdf'):
global x
x += 1
file = os.path.splitext(file)[0]
files.append(file)
print("PDFs found: {0}".format(x))
def makeTmpDir():
if not os.path.exists("tmp"):
os.makedirs("tmp")
print("Temporary directory created.")
def convert(file):
global filename
filename = str(file)
print("Converting {0}...".format(file))
command = 'magick -density 300 -quality 100 "{}.pdf" "{}.jpg"'.format(file, file)
#os.system('cmd /k "magick -density 300 -quality 100 "{}.pdf" "{}.jpg""'.format(file, file))
process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
process.wait()
if process.returncode == 0:
print("Convertion of file {} done.".format(file))
else:
print("Error when trying to convert with ImageMagick.")
exit(0)
def moveJPGs():
for file in os.listdir("."):
if fnmatch.fnmatch(file, "*.jpg"):
shutil.move(file, "tmp/{0}".format(file))
def archive():
print("Making archive...")
for file in os.listdir('tmp'):
if fnmatch.fnmatch(file, '*.jpg'):
shutil.make_archive(filename, "zip", "tmp")
print("Archive done.")
break
else:
print("No jpgs found. Aborting")
exit(0)
def zipToCbr():
print("Start converting zips to CBR...")
for file in os.listdir("."):
if fnmatch.fnmatch(file, "*.zip"):
base = os.path.splitext(file)[0]
os.rename(file, base + ".cbr")
print("{} converted to CBR".format(base))
def cbzToCbr():
print("Start converting CBZ to CBR...")
for file in os.listdir("."):
if fnmatch.fnmatch(file, "*.cbz"):
base = os.path.splitext(file)[0]
os.rename(file, base + ".cbr")
print("{} converted to CBR".format(base))
def deleteTempJPG():
for file in os.listdir("tmp"):
os.remove(os.path.join("tmp", file))
#Actual script
getFiles()
makeTmpDir()
for file in files:
convert(file)
moveJPGs()
archive()
deleteTempJPG()
print(file + " converted to zip.")
zipToCbr()
cbzToCbr()
shutil.rmtree("tmp")
print("Temporary folder cleaned. Process completed.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment