From 6ace0c3b0332815f4ccdf7c57f3232135487c29e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Miquel?= Date: Sun, 8 Feb 2026 15:38:13 +0100 Subject: [PATCH] Changes to page_splitter : get rid of output directory --- page_splitter.py | 59 ++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 50 insertions(+), 9 deletions(-) diff --git a/page_splitter.py b/page_splitter.py index 596812e..71a162f 100644 --- a/page_splitter.py +++ b/page_splitter.py @@ -26,10 +26,10 @@ class PDFPreviewer: self.base_name = os.path.splitext(os.path.basename(self.pdf_path))[0] self.split_dir = f"{self.base_name}_split" self.reorder_dir = f"{self.base_name}_reorder" - if self.output_dir is None: - self.final_file = f"{self.base_name}_final" - else: - self.final_file = f"{self.output_dir}/Copie{self.num:02}.pdf" + + # Create a temporary output file + self.final_file = f"{self.base_name}_temp.pdf" + self.current_page_index = 0 self.page_settings = [] self.processing = False # Flag to prevent multiple finish calls @@ -57,11 +57,24 @@ class PDFPreviewer: if os.path.isdir(path): self.inputs = list_pdf_files(path) - self.output_dir = f"{path}_out" else: - self.inputs = [path] - self.output_dir = None + # Check for existing original in backup and restore if found + dir_name = os.path.dirname(os.path.abspath(path)) + file_name = os.path.basename(path) + backup_path = os.path.join(dir_name, "Copies Originales", file_name) + if os.path.exists(backup_path): + try: + shutil.move(backup_path, path) + print(f"Restored original file from: {backup_path}") + except Exception as e: + messagebox.showerror("Error", f"Failed to restore original file: {e}") + master.destroy() + return + + self.inputs = [path] + + self.output_dir = None self.master = master self.num = 0 self.global_rotation = 0 # Rotation appliquée à tous les fichiers @@ -178,7 +191,7 @@ class PDFPreviewer: # Center the image on the canvas self.canvas.create_image(canvas_width / 2, canvas_height / 2, anchor="center", image=self.photo_img) - + def restart_current_file(self, event=None): """Restarts the processing of the current file.""" # Close the modified in-memory document @@ -273,10 +286,37 @@ class PDFPreviewer: self.master.destroy() def finish_and_process(self): - """Starts the PDF splitting process.""" + """Starts the PDF splitting process and moves files.""" self.split_pdf() self.reorder_pdfs() self.concate_files() + + # Logic to move original to backup and replace with new file + try: + abs_path = os.path.abspath(self.pdf_path) + dir_name = os.path.dirname(abs_path) + file_name = os.path.basename(abs_path) + + backup_dir = os.path.join(dir_name, "Copies Originales") + os.makedirs(backup_dir, exist_ok=True) + + backup_path = os.path.join(backup_dir, file_name) + + # Remove backup if it already exists (overwrite) + if os.path.exists(backup_path): + os.remove(backup_path) + + # Move the original file to "Copies Originales" + shutil.move(self.pdf_path, backup_path) + + # Move the temp output file to replace the original + shutil.move(self.final_file, self.pdf_path) + + print(f"Original moved to {backup_path}, new file saved at {self.pdf_path}") + + except Exception as e: + messagebox.showerror("Error", f"Failed to move/replace files: {e}") + self.remove_dirs() def split_filename_left(self, i): @@ -405,3 +445,4 @@ if __name__ == "__main__": root = tk.Tk() app = PDFPreviewer(root, pdf_file_path) root.mainloop() +