so I made this app several months ago and it was working fine but now when I ran it now, trying to merge 2 pdf files 1.pdf and 2.pdf it, has an error FileNotFoundError: [Errno 2] No such file or directory: '1.pdf' also the when the Banner_Warning is shown the dismiss button does not work anymore
I can print the value of the pdf file below so I don't get why it says no such file:
for pdf_file in e.files:
print(f"{pdf_file}")
UPDATE: i can now merge the file using merger.append(pdf_file.path) somehow merger.append(pdf_file.name) does not work anymore but the file is not saving in the same folder but up one level, so the pdf files are inside Python Scripts\Flet but the merged file is being save in Python Scripts folder, anybody have an idea how to save in the same folder?
below is the code and error output:
from PyPDF2 import PdfWriter
import flet as ft
merger = PdfWriter()
class Banner_Warning(ft.UserControl):
def __init__(self, text_banner: str) -> None:
super().__init__()
self.text_banner = text_banner
def close_banner(self, e: ft.ControlEvent) -> None:
self.banner.open = False
self.update
def build(self) -> ft.Banner:
self.banner = ft.Banner(
bgcolor=ft.colors.RED_500,
leading=ft.Icon(
ft.icons.WARNING_AMBER_ROUNDED, color=ft.colors.AMBER, size=40
),
content=ft.Text(self.text_banner),
actions=[ft.TextButton("Dismiss", on_click=self.close_banner)],
)
self.banner.open = True
return self.banner
def main(page: ft.page):
def merge_pdfs(e: ft.FilePickerResultEvent):
# get file name and password from the corresponding textfields
merge_file_name = textField_name.value
file_password = textField_password1.value
# show warning when no filename is provided
if not merge_file_name or merge_file_name == " ":
# banner for when there is error in file name or file selection
page.add(Banner_Warning("Please check the file name entered."))
return None
# show warning if less than 2 files selected
if not e.files or len(e.files) < 2:
# banner for when there is error in file name or file selection
page.add(Banner_Warning("Please select at least 2 files."))
return None
# merge all selected pdf files
for pdf_file in e.files:
print(f"{pdf_file}")
merger.append(pdf_file.name)
# check if user provided password
if file_password:
merger.encrypt(file_password, use_128bit=True)
# save new pdf file using value of entry input
merger.write(merge_file_name + ".pdf")
merger.close()
# show messagebox merger is complete
page.snack_bar = ft.SnackBar(ft.Text("PDF file merge complete."))
page.snack_bar.open = True
page.update()
pick_files_dialog = ft.FilePicker(on_result=merge_pdfs)
page.overlay.append(pick_files_dialog)
page.title = "Merge PDF"
page.icon = ft.icons.INFO
page.window_width = 320
page.window_height = 300
page.bgcolor = "gray"
page.window_center()
page.window_resizable = False
page.vertical_alignment = "center"
page.horizontal_alignment = "center"
textField_name = ft.TextField(
label="File Name",
autofocus=True,
width=250,
bgcolor="gray",
color="yellow",
border_color="yellow",
)
textField_password1 = ft.TextField(
label="Optional Password",
password=True,
can_reveal_password=True,
autofocus=True,
width=250,
bgcolor="gray",
color="yellow",
border_color="yellow",
)
select_merge_button = ft.ElevatedButton(
"Select and Merge",
icon=ft.icons.UPLOAD_FILE,
on_click=lambda _: pick_files_dialog.pick_files(allow_multiple=True),
color="black",
bgcolor="white",
height=50,
style=ft.ButtonStyle(shape=ft.RoundedRectangleBorder(radius=5)),
)
entriesCol = ft.Column(
spacing=30,
controls=[textField_name, textField_password1, select_merge_button],
horizontal_alignment="center",
)
page.add(entriesCol)
if __name__ == "__main__":
ft.app(target=main)
error output:
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.2288.0_x64__qbz5n2kfra8p0\Lib\threading.py", line 1045, in _bootstrap_inner
self.run()
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.2288.0_x64__qbz5n2kfra8p0\Lib\threading.py", line 982, in run
self._target(*self._args, **self._kwargs)
File "C:\Users\GV\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\flet_core\event_handler.py", line 28, in __sync_handler
h(r)
File "c:\Users\GV\Box\Documents\Python Scripts\Flet\flet_merge_pdf_oop.py", line 53, in merge_pdfs
merger.append(pdf_file.name)
File "C:\Users\GV\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\PyPDF2\_writer.py", line 2326, in append
self.merge(
File "C:\Users\GV\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\PyPDF2\_utils.py", line 417, in wrapper
return func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\GV\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\PyPDF2\_writer.py", line 2372, in merge
stream, encryption_obj = self._create_stream(fileobj)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\GV\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\PyPDF2\_writer.py", line 2253, in _create_stream
with FileIO(fileobj, "rb") as f:
^^^^^^^^^^^^^^^^^^^^^
FileNotFoundError: [Errno 2] No such file or directory: '1.pdf'