Advertisement
alekssamos

extract_text 2.0

Apr 20th, 2024
496
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.08 KB | None | 0 0
  1. from threading import Thread
  2. import wx
  3. import PyPDF2
  4. import os
  5.  
  6. class MyFrame(wx.Frame):
  7.     def __init__(self, parent, title):
  8.         super().__init__(parent, title=title)
  9.         self.panel = wx.Panel(self)
  10.         vbox = wx.BoxSizer()
  11.         self.static1 = wx.StaticText(self.panel, label="Please wait")
  12.         self.gauge = wx.Gauge(self.panel, range=100, style=wx.GA_HORIZONTAL)
  13.         self.gauge.SetCanFocus(True)
  14.         vbox.Add(self.static1, flag=wx.ALL, border=0)
  15.         vbox.Add(self.gauge, flag=wx.ALL, border=10)
  16.         self.panel.SetSizer(vbox)
  17.         open_dlg = wx.FileDialog(self.panel, "open file", os.getcwd(), "", "*.pdf", wx.FD_OPEN)
  18.         if open_dlg.ShowModal() == wx.ID_CANCEL: self.Destroy()
  19.         self.file_name = open_dlg.GetFilename()
  20.         dir_name = open_dlg.GetDirectory()
  21.         source_file_path = os.path.join(dir_name, self.file_name)
  22.         open_dlg.Destroy()
  23.         self.file_name= self.file_name.split(".")
  24.         self.file_name[1] = ".txt"
  25.         self.file_name = "".join(self.file_name)
  26.         save_dlg = wx.DirDialog(self.panel, "save file", defaultPath="")
  27.         if save_dlg.ShowModal() == wx.ID_CANCEL: self.Destroy()
  28.         destination_file_path = os.path.join(save_dlg.GetPath(), self.file_name)
  29.         save_dlg.Destroy()
  30.         self.thr = Thread(target=self.show_progress, args=(source_file_path, destination_file_path))
  31.         self.thr.start()
  32.     def show_progress(self, source_file_path, destination_file_path):
  33.         with open(source_file_path, "rb") as s_file:
  34.             with open(destination_file_path, "w", encoding="utf-8") as d_file:
  35.                 reader = PyPDF2.PdfReader(s_file)
  36.                 for i in range(len(reader.pages)):
  37.                     page = reader.pages[i]
  38.                     d_file.write(page.extract_text())
  39.                     progress = int((i + 1) / len(reader.pages) * 100)
  40.                     self.gauge.SetValue(progress)
  41.         self.Destroy()
  42.  
  43.  
  44. app = wx.App()
  45. my_frame = MyFrame(None, "test")
  46. my_frame.Show()
  47. my_frame.gauge.SetFocus()
  48. my_frame.SetFocus()
  49. app.MainLoop()
  50.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement