Added exception logic. Records failed conversions into excel file. Now steps through excel file to get the PDF path and promail number. Also, copies and renames PDF.

This commit is contained in:
Dan Dembinski
2021-05-01 19:31:38 -04:00
parent fbc31e9c62
commit fd4ac10a90
2 changed files with 53 additions and 25 deletions

76
main.py
View File

@@ -1,28 +1,54 @@
import pdf2image import pdf2image, shutil
from openpyxl import load_workbook
path = 'C:\\Users\\ddembinski\\Desktop\\'
outputPath = 'C:\\Users\\ddembinski\\Desktop\\thumb\\'
file = 'test.pdf'
# Gets PDF info, splits the 'Page size' key by the space and sets the width and height values path = 'U:\\CreativeConnectionMove\\Assets\\'
info = pdf2image.pdfinfo_from_path('C:\\Users\\ddembinski\\Desktop\\test.pdf', poppler_path='c:\\poppler\\bin') # outputPath = 'C:\\Users\\ddembinski\\Desktop\\thumb\\'
# print(info['Page size'].split(' ')) outputPath = 'U:\\thumb\\'
width = info['Page size'].split(' ')[0]
height = info['Page size'].split(' ')[2]
# Landscape
if int(width) > int(height): wb = load_workbook('fomoPDFs.xlsx')
images = pdf2image.convert_from_path(path + file, poppler_path='c:\\poppler\\bin', sh1 = wb.get_sheet_by_name('Sheet1')
output_folder=outputPath + '512', single_file=True, sh2 = wb.get_sheet_by_name('Failed')
size=(512,None), fmt='jpeg', output_file=file) failedrow = 2
images = pdf2image.convert_from_path(path + file, poppler_path='c:\\poppler\\bin',
output_folder=outputPath + '128', single_file=True, for row in sh1.iter_rows(min_row=2):
size=(128, None), fmt='jpeg', output_file=file)
# Portrait or square file = row[3].value
else: outfile = row[2].value
images = pdf2image.convert_from_path(path + file, poppler_path='c:\\poppler\\bin',
output_folder=outputPath + '512',single_file=True, print(row[0].value)
size=(None,512), fmt='jpeg', output_file=file)
images = pdf2image.convert_from_path(path + file, poppler_path='c:\\poppler\\bin', # Gets PDF info, splits the 'Page size' key by the space and sets the width and height values
output_folder=outputPath + '128', single_file=True, try:
size=(None, 128), fmt='jpeg', output_file=file) info = pdf2image.pdfinfo_from_path(path + file, poppler_path='c:\\poppler\\bin')
# print(info['Page size'].split(' '))
width = info['Page size'].split(' ')[0]
height = info['Page size'].split(' ')[2]
# Landscape
if float(width) > float(height):
images = pdf2image.convert_from_path(path + file, poppler_path='c:\\poppler\\bin',
output_folder=outputPath + '512', single_file=True,
size=(512,None), fmt='jpeg', output_file=outfile)
images = pdf2image.convert_from_path(path + file, poppler_path='c:\\poppler\\bin',
output_folder=outputPath + '128', single_file=True,
size=(128, None), fmt='jpeg', output_file=outfile)
# Portrait or square
else:
images = pdf2image.convert_from_path(path + file, poppler_path='c:\\poppler\\bin',
output_folder=outputPath + '512',single_file=True,
size=(None,512), fmt='jpeg', output_file=outfile)
images = pdf2image.convert_from_path(path + file, poppler_path='c:\\poppler\\bin',
output_folder=outputPath + '128', single_file=True,
size=(None, 128), fmt='jpeg', output_file=outfile)
shutil.copy(path + file, outputPath + 'PDF\\' + outfile + '.pdf')
except:
print('%s - %s conversion failed' % (row[0].value, row[3].value))
sh2.cell(row=failedrow, column=1).value = row[0].value
sh2.cell(row=failedrow, column=2).value = row[2].value
sh2.cell(row=failedrow, column=3).value = row[3].value
failedrow = failedrow + 1
wb.save('fomoPDFs.xlsx')

View File

@@ -1,2 +1,4 @@
et-xmlfile==1.1.0
openpyxl==3.0.7
pdf2image==1.14.0 pdf2image==1.14.0
Pillow==8.2.0 Pillow==8.2.0