Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import csv
- import os
- import time
- import wx
- from tika import parser
- class MyFrame(wx.Frame):
- content = []
- numbers = []
- def __init__(self):
- super().__init__(parent=None, title='PDF-CSV(compare app)',
- style=wx.DEFAULT_FRAME_STYLE & ~(wx.RESIZE_BORDER | wx.MAXIMIZE_BOX))
- panel = wx.Panel(self)
- self.Centre()
- self.style = wx.DEFAULT_FRAME_STYLE & ~(wx.RESIZE_BORDER | wx.MAXIMIZE_BOX)
- my_sizer = wx.BoxSizer(wx.VERTICAL)
- btnLoadIssue = wx.Button(panel, label='Load Issue')
- btnLoadIssue.Bind(wx.EVT_BUTTON, self.OnOpen_PDF)
- my_sizer.Add(btnLoadIssue, 0, wx.ALL | wx.CENTER, 7)
- btnLoadSerials = wx.Button(panel, label='Load Serials')
- btnLoadSerials.Bind(wx.EVT_BUTTON, self.OnOpen_CSV)
- my_sizer.Add(btnLoadSerials, 0, wx.ALL | wx.CENTER, 7)
- btnCompare = wx.Button(panel, label='Compare')
- btnCompare.Bind(wx.EVT_BUTTON, self.On_Compare)
- my_sizer.Add(btnCompare, 0, wx.ALL | wx.CENTER, 7)
- btnExit = wx.Button(panel, label='Exit')
- btnExit.Bind(wx.EVT_BUTTON, self.onClose)
- my_sizer.Add(btnExit, 0, wx.ALL | wx.CENTER, 7)
- panel.SetSizer(my_sizer)
- self.Show()
- def OnOpen_PDF(self, event):
- print('pdf')
- global content
- # otherwise ask the user what new file to open
- with wx.FileDialog(self, "Open PDF file", wildcard="PDF files (*.pdf)|*.pdf",
- style=wx.FD_OPEN | wx.FD_FILE_MUST_EXIST) as fileDialog:
- if fileDialog.ShowModal() == wx.ID_CANCEL:
- return # the user changed their mind
- # Proceed loading the file chosen by the user
- pdf_filename = fileDialog.GetPath()
- try:
- content = get_text_from_pdf(pdf_filename)
- print(content)
- except:
- print('Can not load CSV file!')
- content = []
- def OnOpen_CSV(self, event):
- print('csv')
- global numbers
- # otherwise ask the user what new file to open
- with wx.FileDialog(self, "Open CSV file", wildcard="CSV files (*.csv)|*.csv",
- style=wx.FD_OPEN | wx.FD_FILE_MUST_EXIST) as fileDialog:
- if fileDialog.ShowModal() == wx.ID_CANCEL:
- return # the user changed their mind
- # Proceed loading the file chosen by the user
- csv_filename = fileDialog.GetPath()
- try:
- numbers = get_numbers(csv_filename)
- except:
- print('Can not load CSV file!')
- numbers = []
- def On_Compare(self, event):
- print('Compare')
- try:
- if len(numbers) > 0 and len(content) > 0:
- print('Results:')
- compare(numbers, content)
- print('Files been processed successfully!')
- print('Time:', time.time() - st, 'sec')
- except:
- print('Can not compare files now!')
- def onClose(self, event):
- print('Close')
- self.Close()
- def get_numbers(csv_filename):
- with open(csv_filename, newline='') as csvfile:
- spamreader = csv.reader(csvfile, delimiter=' ', quotechar='|')
- for row in spamreader:
- numbers.append(row[0].replace('"', ''))
- print('Numbers:', numbers)
- return numbers
- def get_text_from_pdf(pdf_filename):
- raw = parser.from_file(pdf_filename)
- content = raw['content']
- print('PDF file content:')
- print(content)
- return content
- def compare(numbers, content):
- if os.path.exists("Confirmed_ads.txt"):
- os.remove("Confirmed_ads.txt")
- if os.path.exists("Missing.txt"):
- os.remove("Missing.txt")
- for number in numbers:
- if number in content:
- print(number, 'YES')
- with open("Confirmed_ads.txt", "a") as myfile:
- myfile.write(number + '\n')
- else:
- print(number, 'NO')
- with open("Missing.txt", "a") as myfile:
- myfile.write(number + '\n')
- if __name__ == '__main__':
- app = wx.App()
- frame = MyFrame()
- frame.Show()
- app.MainLoop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement