Advertisement
Guest User

Untitled

a guest
Aug 21st, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.25 KB | None | 0 0
  1. import csv
  2. import os
  3. import time
  4. import wx
  5. from tika import parser
  6.  
  7.  
  8. class MyFrame(wx.Frame):
  9.     content = []
  10.     numbers = []
  11.  
  12.     def __init__(self):
  13.  
  14.         super().__init__(parent=None, title='PDF-CSV(compare app)',
  15.                          style=wx.DEFAULT_FRAME_STYLE & ~(wx.RESIZE_BORDER | wx.MAXIMIZE_BOX))
  16.         panel = wx.Panel(self)
  17.         self.Centre()
  18.         self.style = wx.DEFAULT_FRAME_STYLE & ~(wx.RESIZE_BORDER | wx.MAXIMIZE_BOX)
  19.  
  20.         my_sizer = wx.BoxSizer(wx.VERTICAL)
  21.  
  22.         btnLoadIssue = wx.Button(panel, label='Load Issue')
  23.         btnLoadIssue.Bind(wx.EVT_BUTTON, self.OnOpen_PDF)
  24.         my_sizer.Add(btnLoadIssue, 0, wx.ALL | wx.CENTER, 7)
  25.  
  26.         btnLoadSerials = wx.Button(panel, label='Load Serials')
  27.         btnLoadSerials.Bind(wx.EVT_BUTTON, self.OnOpen_CSV)
  28.         my_sizer.Add(btnLoadSerials, 0, wx.ALL | wx.CENTER, 7)
  29.  
  30.         btnCompare = wx.Button(panel, label='Compare')
  31.         btnCompare.Bind(wx.EVT_BUTTON, self.On_Compare)
  32.         my_sizer.Add(btnCompare, 0, wx.ALL | wx.CENTER, 7)
  33.  
  34.         btnExit = wx.Button(panel, label='Exit')
  35.         btnExit.Bind(wx.EVT_BUTTON, self.onClose)
  36.         my_sizer.Add(btnExit, 0, wx.ALL | wx.CENTER, 7)
  37.  
  38.         panel.SetSizer(my_sizer)
  39.         self.Show()
  40.  
  41.     def OnOpen_PDF(self, event):
  42.         print('pdf')
  43.         global content
  44.         # otherwise ask the user what new file to open
  45.         with wx.FileDialog(self, "Open PDF file", wildcard="PDF files (*.pdf)|*.pdf",
  46.                            style=wx.FD_OPEN | wx.FD_FILE_MUST_EXIST) as fileDialog:
  47.  
  48.             if fileDialog.ShowModal() == wx.ID_CANCEL:
  49.                 return  # the user changed their mind
  50.  
  51.             # Proceed loading the file chosen by the user
  52.             pdf_filename = fileDialog.GetPath()
  53.             try:
  54.                 content = get_text_from_pdf(pdf_filename)
  55.                 print(content)
  56.             except:
  57.                 print('Can not load CSV file!')
  58.                 content = []
  59.  
  60.     def OnOpen_CSV(self, event):
  61.         print('csv')
  62.         global numbers
  63.         # otherwise ask the user what new file to open
  64.         with wx.FileDialog(self, "Open CSV file", wildcard="CSV files (*.csv)|*.csv",
  65.                            style=wx.FD_OPEN | wx.FD_FILE_MUST_EXIST) as fileDialog:
  66.  
  67.             if fileDialog.ShowModal() == wx.ID_CANCEL:
  68.                 return  # the user changed their mind
  69.  
  70.             # Proceed loading the file chosen by the user
  71.             csv_filename = fileDialog.GetPath()
  72.             try:
  73.                 numbers = get_numbers(csv_filename)
  74.             except:
  75.                 print('Can not load CSV file!')
  76.                 numbers = []
  77.  
  78.     def On_Compare(self, event):
  79.         print('Compare')
  80.         try:
  81.             if len(numbers) > 0 and len(content) > 0:
  82.                 print('Results:')
  83.                 compare(numbers, content)
  84.                 print('Files been processed successfully!')
  85.                 print('Time:', time.time() - st, 'sec')
  86.         except:
  87.             print('Can not compare files now!')
  88.  
  89.     def onClose(self, event):
  90.         print('Close')
  91.         self.Close()
  92.  
  93.  
  94. def get_numbers(csv_filename):
  95.     with open(csv_filename, newline='') as csvfile:
  96.         spamreader = csv.reader(csvfile, delimiter=' ', quotechar='|')
  97.         for row in spamreader:
  98.             numbers.append(row[0].replace('"', ''))
  99.  
  100.     print('Numbers:', numbers)
  101.     return numbers
  102.  
  103.  
  104. def get_text_from_pdf(pdf_filename):
  105.     raw = parser.from_file(pdf_filename)
  106.     content = raw['content']
  107.     print('PDF file content:')
  108.     print(content)
  109.     return content
  110.  
  111.  
  112. def compare(numbers, content):
  113.     if os.path.exists("Confirmed_ads.txt"):
  114.         os.remove("Confirmed_ads.txt")
  115.  
  116.     if os.path.exists("Missing.txt"):
  117.         os.remove("Missing.txt")
  118.  
  119.     for number in numbers:
  120.         if number in content:
  121.             print(number, 'YES')
  122.             with open("Confirmed_ads.txt", "a") as myfile:
  123.                 myfile.write(number + '\n')
  124.         else:
  125.             print(number, 'NO')
  126.             with open("Missing.txt", "a") as myfile:
  127.                 myfile.write(number + '\n')
  128.  
  129.  
  130. if __name__ == '__main__':
  131.     app = wx.App()
  132.     frame = MyFrame()
  133.     frame.Show()
  134.     app.MainLoop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement