dev #1

Merged
tidoni merged 7 commits from dev into main 2024-01-23 16:42:13 +00:00
2 changed files with 80 additions and 9 deletions
Showing only changes of commit 376fafa171 - Show all commits

View File

@ -1,9 +1,48 @@
from pypdf import PdfReader, PdfWriter
import uuid
import os
import shutil
import datetime as dt
import logging
import sys
from pdf_util.pdf_util import pdf_util
# Setup Logging
logging.basicConfig(
# level=logging.ERROR,
# level=logging.INFO,
level=logging.DEBUG,
format=str(dt.datetime.now()).replace(" ", "_") + " | %(asctime)s [%(levelname)s] %(message)s",
handlers=[
logging.FileHandler("/var/log/" + str(dt.datetime.today().strftime('%Y-%m-%d')) + "_-_pdf_project_manager.log"),
logging.StreamHandler(sys.stdout)
]
)
base_path = "/app/projects/"
class pdf_project_manager:
def __init__(self):
self.uuid = str(uuid.uuid4())
os.makedirs("/app/projects/" + self.uuid, exist_ok=True)
self.pdf_init = False
self.project_name = ""
os.makedirs(base_path + self.uuid, exist_ok=True)
"""
def add_pdf(self, pdf_path):
if self.pdf_init:
shutil.copyfile(pdf_path, base_path + self.uuid + "/complete.pdf")
else:
shutil.copyfile(pdf_path, base_path + self.uuid + "/tmp.pdf")
pdf_util(base_path + self.uuid + "/complete.pdf").merge_pdf_with_and_location(base_path + self.uuid + "/tmp.pdf", base_path + self.uuid + "/tmp_complete.pdf")
shutil.copyfile(base_path + self.uuid + "/tmp_complete.pdf", base_path + self.uuid + "/complete.pdf")
os.remove(base_path + self.uuid + "/tmp_complete.pdf")
os.remove(base_path + self.uuid + "/tmp.pdf")
# Splitt files in all single Pages in Subdirectory...
"""

View File

@ -1,6 +1,23 @@
import os
from pypdf import PdfReader, PdfWriter
import datetime as dt
import logging
import sys
# Setup Logging
logging.basicConfig(
# level=logging.ERROR,
# level=logging.INFO,
level=logging.DEBUG,
format=str(dt.datetime.now()).replace(" ", "_") + " | %(asctime)s [%(levelname)s] %(message)s",
handlers=[
logging.FileHandler("/var/log/" + str(dt.datetime.today().strftime('%Y-%m-%d')) + "_-_pdf_util.log"),
logging.StreamHandler(sys.stdout)
]
)
class pdf_util:
def __init__(self, file_path):
self.file_path = file_path
@ -8,9 +25,9 @@ class pdf_util:
self.file_name_wo_extension = os.path.splitext(os.path.basename(file_path))[0]
def split_pdf(self):
def split_pdf_with_location(self, output_filepath, no_names=False):
out_filenames = []
os.makedirs(os.path.dirname(self.file_path) + "/split_pdf", exist_ok=True)
os.makedirs(os.path.dirname(output_filepath), exist_ok=True)
with open(self.file_path, 'rb') as pdf_file:
pdf_reader = PdfReader(pdf_file)
num_pages = len(pdf_reader.pages)
@ -19,16 +36,25 @@ class pdf_util:
writer = PdfWriter()
writer.add_page(pdf_reader.pages[page_num])
out_filename = os.path.dirname(self.file_path) + '/split_pdf/' + self.file_name_wo_extension + '_' + str(page_num + 1) + '.pdf'
if no_names:
out_filename = os.path.dirname(output_filepath) + '/' + str(page_num + 1) + '.pdf'
else:
out_filename = os.path.dirname(output_filepath) + '/' + self.file_name_wo_extension + '_' + str(page_num + 1) + '.pdf'
with open(out_filename, 'wb') as outfile:
writer.write(outfile)
out_filenames.append(out_filename)
return out_filenames
# Deprecate when pdf_project_manager takes effect
def split_pdf(self):
os.makedirs(os.path.dirname(self.file_path) + "/split_pdf", exist_ok=True)
return self.split_pdf_with_location(os.path.dirname(self.file_path) + "/split_pdf/", False)
def merge_pdf_with(self, merge_file_path, merged_name="merged"):
os.makedirs(os.path.dirname(self.file_path) + "/merge_pdf", exist_ok=True)
def merge_pdf_with_and_location(self, merge_file_path, output_filepath):
os.makedirs(os.path.dirname(output_filepath), exist_ok=True)
pdf_reader_1 = PdfReader(self.file_path)
pdf_reader_2 = PdfReader(merge_file_path)
writer = PdfWriter()
@ -39,8 +65,14 @@ class pdf_util:
for page_num in range(len(pdf_reader_2.pages)):
writer.add_page(pdf_reader_2.pages[page_num])
out_path = os.path.dirname(self.file_path) + "/merge_pdf" + '/merger.pdf'
with open(out_path, 'wb') as outfile:
with open(output_filepath, 'wb') as outfile:
writer.write(outfile)
return out_path
return output_filepath
# Deprecate when pdf_project_manager takes effect
def merge_pdf_with(self, merge_file_path, merged_name="merged"):
os.makedirs(os.path.dirname(self.file_path) + "/merge_pdf", exist_ok=True)
return self.merge_pdf_with_and_location(merge_file_path, os.path.dirname(self.file_path) + "/merge_pdf" + '/merger.pdf')