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
info = pdf2image.pdfinfo_from_path('C:\\Users\\ddembinski\\Desktop\\test.pdf', poppler_path='c:\\poppler\\bin')
# print(info['Page size'].split(' '))
width = info['Page size'].split(' ')[0]
height = info['Page size'].split(' ')[2]
path = 'U:\\CreativeConnectionMove\\Assets\\'
# outputPath = 'C:\\Users\\ddembinski\\Desktop\\thumb\\'
outputPath = 'U:\\thumb\\'
# Landscape
if int(width) > int(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=file)
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=file)
# 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=file)
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=file)
wb = load_workbook('fomoPDFs.xlsx')
sh1 = wb.get_sheet_by_name('Sheet1')
sh2 = wb.get_sheet_by_name('Failed')
failedrow = 2
for row in sh1.iter_rows(min_row=2):
file = row[3].value
outfile = row[2].value
print(row[0].value)
# Gets PDF info, splits the 'Page size' key by the space and sets the width and height values
try:
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')