Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import flet as ft
- import PyPDF2
- from PyPDF2 import PdfWriter
- merger = PdfWriter()
- def main(page: ft.page):
- def close_banner(e):
- page.banner.open = False
- page.update()
- def show_banner(e):
- page.banner.open = True
- page.update()
- 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.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("Please check the file name entered."),
- actions=[ft.TextButton("Dismiss", on_click=close_banner)])
- show_banner(e)
- 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.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("Please select at least 2 files."),
- actions=[ft.TextButton("Dismiss", on_click=close_banner)])
- show_banner(e)
- return None
- # merge all selected pdf files
- for pdf_file in e.files:
- 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__":
- # app as a desktop app
- ft.app(target=main)
- # app as a web app
- # ft.app(target=main, view=ft.WEB_BROWSER)
Advertisement
Add Comment
Please, Sign In to add comment