Skip to content

Instantly share code, notes, and snippets.

@Misterguruman
Created October 14, 2022 16:41
Show Gist options
  • Save Misterguruman/8b158dee5c0de37524ae25eec561ad53 to your computer and use it in GitHub Desktop.
Save Misterguruman/8b158dee5c0de37524ae25eec561ad53 to your computer and use it in GitHub Desktop.
A script written to correct a file management software incorrectly flipping images on a file server. This would scan certain directories and correct the pdfs.
from PyPDF2 import PdfFileWriter, PdfFileReader
import os, datetime, time
cwd = os.getcwd()
root_dir = cwd
main_dir = cwd + '\\00\\00'
log_path = cwd + '\\PeDeEfLog'
log_file_path = log_path + '\\log.txt'
class Parser():
def __init__(self, pdfnum):
self.folders = os.listdir(main_dir)
self.log_file = ''
self.current_folder = ''
self.current_temp_folder = ''
self.current_folder_files = ''
self.current_file_log = ''
self.current_file_log_contents = ''
self.number_of_pdfs = pdfnum
def log(self, message):
ts = time.time()
time_stamp = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
self.log_file.write(time_stamp + ' : ' + message + '\n')
def log_pdf(self, pdf):
self.current_file_log.write(pdf+'\n')
def log_check(self):
if os.path.isdir(log_path):
print "Log Path Exists"
else:
os.mkdir(log_path)
print "Created Log Path"
if os.path.isfile(log_file_path):
print "Log File Exists"
else:
try:
f = open(log_file_path, "w")
except IOError:
pass
f.close()
print "Created Log File"
self.log_file = open(log_file_path, "a")
def file_check(self):
self.current_file_log = self.current_temp_folder + '\\files.txt'
if os.path.isfile(self.current_file_log):
self.log(str(self.current_file_log)+ "Temp file exists")
x = open(self.current_file_log, "r")
self.current_file_log_contents = x.read().split('\n')
x.close()
else:
try:
f = open(self.current_file_log, "w")
except IOError:
pass
f.close()
self.log("File created")
self.current_file_log = open(self.current_file_log, "a")
def temp_check(self):
if 'temp' in self.current_folder_files:
self.current_temp_folder = self.current_folder +'\\temp'
self.file_check()
self.log('Directory Exists')
else:
os.mkdir(self.current_folder +'\\temp')
self.current_temp_folder = self.current_folder +'\\temp'
self.file_check()
self.log(str(self.current_temp_folder)+'Temp directory Created')
def alter_pdf(self, pdf):
pdf_path = self.current_folder + '\\' + pdf
destination = self.current_temp_folder + '\\' + pdf
output = PdfFileWriter()
with open(pdf_path, "rb") as f:
input = PdfFileReader(pdf_path, 'rb')
pagecount = int(input.getNumPages())
p = input.getPage(0)
(w, h) = p.mediaBox.upperRight
for page in range(pagecount):
# print page
if page is 0 or page is pagecount - self.number_of_pdfs:
# print "no change"
output.addPage(input.getPage(page))
else:
# print "change"
p = input.getPage(page)
p.scaleTo(w, h)
output.addPage(p)
os.rename(pdf_path, destination)
with open(pdf_path, 'wb') as f:
output.write(f)
def run(self):
self.log_check()
self.log("Script Started.")
self.log("Log Check Complete.")
for folder in self.folders:
self.current_folder = main_dir+'\\'+folder
self.log('Processing '+str(self.current_folder))
self.current_folder_files = os.listdir(self.current_folder)
self.temp_check()
for pdf in self.current_folder_files:
if pdf.endswith('.pdf'):
if pdf in self.current_file_log_contents:
self.log("The file " + pdf + " is in file log")
else:
self.log(pdf +" "+ "File is not in file log")
self.alter_pdf(pdf)
self.log_pdf(pdf)
else:
self.log(pdf +"File is not a pdf, skipping...")
self.log("Script ending, closing log files...")
self.log_file.close()
self.current_file_log.close()
os.chdir(root_dir +'\\Accounting\\Credit Cards\\Credit Card Charges Forms')
cwd = os.getcwd()
main_dir = cwd + '\\00\\00'
log_file_path = log_path + '\\log.txt'
if os.path.isdir(main_dir):
x = Parser(1)
x.run()
x = None
os.chdir(root_dir +'\\Accounting\\Accounts Payable\\Purchase Requisitions')
cwd = os.getcwd()
main_dir = cwd + '\\00\\00'
log_file_path = log_path + '\\log.txt'
if os.path.isdir(main_dir):
x = Parser(1)
x.run()
x = None
os.chdir(root_dir +'\\Accounting\\Credit Cards\\Travel Expense Forms')
cwd = os.getcwd()
main_dir = cwd + '\\00\\00'
log_file_path = log_path + '\\log.txt'
if os.path.isdir(main_dir):
x = Parser(2)
x.run()
x = None
os.chdir(root_dir +'\\Accounting\\Credit Cards\\Employee Travel Statements')
cwd = os.getcwd()
main_dir = cwd + '\\00\\00'
log_file_path = log_path + '\\log.txt'
if os.path.isdir(main_dir):
x = Parser(2)
x.run()
x = None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment