Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from threading import Thread
- import wx
- import PyPDF2
- import os
- class MyFrame(wx.Frame):
- def __init__(self, parent, title):
- super().__init__(parent, title=title)
- self.panel = wx.Panel(self)
- vbox = wx.BoxSizer()
- self.static1 = wx.StaticText(self.panel, label="Please wait")
- self.gauge = wx.Gauge(self.panel, range=100, style=wx.GA_HORIZONTAL)
- self.gauge.SetCanFocus(True)
- vbox.Add(self.static1, flag=wx.ALL, border=0)
- vbox.Add(self.gauge, flag=wx.ALL, border=10)
- self.panel.SetSizer(vbox)
- open_dlg = wx.FileDialog(self.panel, "open file", os.getcwd(), "", "*.pdf", wx.FD_OPEN)
- if open_dlg.ShowModal() == wx.ID_CANCEL: self.Destroy()
- self.file_name = open_dlg.GetFilename()
- dir_name = open_dlg.GetDirectory()
- source_file_path = os.path.join(dir_name, self.file_name)
- open_dlg.Destroy()
- self.file_name= self.file_name.split(".")
- self.file_name[1] = ".txt"
- self.file_name = "".join(self.file_name)
- save_dlg = wx.DirDialog(self.panel, "save file", defaultPath="")
- if save_dlg.ShowModal() == wx.ID_CANCEL: self.Destroy()
- destination_file_path = os.path.join(save_dlg.GetPath(), self.file_name)
- save_dlg.Destroy()
- self.thr = Thread(target=self.show_progress, args=(source_file_path, destination_file_path))
- self.thr.start()
- def show_progress(self, source_file_path, destination_file_path):
- with open(source_file_path, "rb") as s_file:
- with open(destination_file_path, "w", encoding="utf-8") as d_file:
- reader = PyPDF2.PdfReader(s_file)
- for i in range(len(reader.pages)):
- page = reader.pages[i]
- d_file.write(page.extract_text())
- progress = int((i + 1) / len(reader.pages) * 100)
- self.gauge.SetValue(progress)
- self.Destroy()
- app = wx.App()
- my_frame = MyFrame(None, "test")
- my_frame.Show()
- my_frame.gauge.SetFocus()
- my_frame.SetFocus()
- app.MainLoop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement