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:
60
main.py
60
main.py
@@ -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')
|
||||||
|
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',
|
images = pdf2image.convert_from_path(path + file, poppler_path='c:\\poppler\\bin',
|
||||||
output_folder=outputPath + '512', single_file=True,
|
output_folder=outputPath + '512', single_file=True,
|
||||||
size=(512,None), fmt='jpeg', output_file=file)
|
size=(512,None), fmt='jpeg', output_file=outfile)
|
||||||
images = pdf2image.convert_from_path(path + file, poppler_path='c:\\poppler\\bin',
|
images = pdf2image.convert_from_path(path + file, poppler_path='c:\\poppler\\bin',
|
||||||
output_folder=outputPath + '128', single_file=True,
|
output_folder=outputPath + '128', single_file=True,
|
||||||
size=(128, None), fmt='jpeg', output_file=file)
|
size=(128, None), fmt='jpeg', output_file=outfile)
|
||||||
# Portrait or square
|
# Portrait or square
|
||||||
else:
|
else:
|
||||||
images = pdf2image.convert_from_path(path + file, poppler_path='c:\\poppler\\bin',
|
images = pdf2image.convert_from_path(path + file, poppler_path='c:\\poppler\\bin',
|
||||||
output_folder=outputPath + '512',single_file=True,
|
output_folder=outputPath + '512',single_file=True,
|
||||||
size=(None,512), fmt='jpeg', output_file=file)
|
size=(None,512), fmt='jpeg', output_file=outfile)
|
||||||
images = pdf2image.convert_from_path(path + file, poppler_path='c:\\poppler\\bin',
|
images = pdf2image.convert_from_path(path + file, poppler_path='c:\\poppler\\bin',
|
||||||
output_folder=outputPath + '128', single_file=True,
|
output_folder=outputPath + '128', single_file=True,
|
||||||
size=(None, 128), fmt='jpeg', output_file=file)
|
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')
|
||||||
|
|||||||
Reference in New Issue
Block a user