forked from vfi27/pdf-editor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
132 lines (103 loc) · 4.12 KB
/
app.py
File metadata and controls
132 lines (103 loc) · 4.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
from core.reorder import reorder_pages
import questionary
import sys
from rich import print
from rich.panel import Panel
from rich.console import Console
from core.merge import merge_files
from rich.align import Align
from PyQt6.QtWidgets import QApplication
from core.gui_merge import MainWindow
from core.gui_reorder import ReorderWindow
# Initialize Console
console = Console()
def main():
# Print title
title_panel = Panel(
"[bold cyan]Welcome to PDF Toolkit![/bold cyan]",
border_style="cyan",
padding=(1, 5), # Adds 1 line of vertical padding, 5 spaces of horizontal padding
expand=False
)
console.print("\n")
console.print(Align.center(title_panel))
console.print("\n")
# Print Info Note
note_panel = Panel(
"Press [bold white]Enter [/bold white]to select. \n [bold white]Use Ctrl + C [/bold white](on Windows & macOS) to abort.",
title="[bold yellow]💡 Note[/bold yellow]",
border_style="yellow",
style="yellow",
expand=False
)
console.print(note_panel)
console.print("\n")
# Ask user about type of operation
operation = questionary.select(
"What would you like to do?",
choices=["Merge PDFs",
"Reorder PDF",
"Exit"
]).ask()
if (operation == "Exit"):
# Exit program
console.print("See you later!")
return
elif (operation == "Merge PDFs"):
app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec()
# Retrieve selected files from the GUI
selected_files = window.selected_files
if not selected_files:
console.print("[bold red]No files selected. Operation cancelled.[/bold red]\n")
return
output_name = "merged_output.pdf"
output_path = None
if getattr(window, "out_path", None):
output_path = window.out_path
output_name = window.out_path.name
console.print(f"Output will be saved to: {output_path}")
else:
console.print(f"Output will be saved to default Downloads path as: {output_name}")
try:
merge_files(selected_files, output_name, output_path)
console.print("[bold green]Merged successfully![/bold green]\n")
except FileNotFoundError:
console.print("[bold red]Error: One of the files you typed does not exist.[/bold red]\n")
except Exception as e:
console.print(f"[bold red]An unexpected error occurred: {e}[/bold red]\n")
elif (operation == "Reorder PDF"):
app = QApplication(sys.argv)
window = ReorderWindow()
window.show()
app.exec()
# Retrieve selected files from the GUI
selected_files = window.selected_files
# Check if they selected nothing
if not selected_files:
console.print("[bold red]No files selected. Operation cancelled.[/bold red]\n")
return
# Check if they selected too many files
if len(selected_files) > 1:
console.print("[bold red]Error: Please select only ONE file to reorder.[/bold red]\n")
return
# Extract the single target file
target_file = selected_files[0]
console.print(f"\n[yellow]Preparing to reorder: {target_file}[/yellow]")
output_name = "reordered_output.pdf"
output_path = None
if getattr(window, "out_path", None):
output_path = window.out_path
output_name = window.out_path.name
console.print(f"Output will be saved to: {output_path}")
else:
console.print(f"Output will be saved to default Downloads path as: {output_name}")
try:
reorder_pages(target_file, output_name, output_path)
console.print("[bold green]Reordered successfully![/bold green]\n")
except FileNotFoundError:
console.print("[bold red]Error: One of the files you typed does not exist.[/bold red]\n")
except Exception as e:
console.print(f"[bold red]An unexpected error occurred: {e}[/bold red]\n")