I have the following Python script. The purpose of the script is to extract text from PDFs. I use textract for that because soon I realized there is no easy way to check if a page contains an image or not. So I extract the whole text using textract.
The workflow is like this. main()
parses each pdf file from a folder, I extract the text, I search for keyword strikes and then I export the result to a csv file inside folder output_results
.
What I can fix on my logic? What can I change in my code? I find it messy, how I can clean it up?
import textract
import os
import csv
class PdfMiner():
path = os.getcwd() + '/folderForPdf/'
output_path = os.getcwd() + '/output_results/'
def __init__(self):
pass
def main(self):
for self.filename in os.listdir(self.path):
self.text = (PdfMiner().extract_text_from_pdf(self.path + self.filename))
self.keyword_strike_dict = PdfMiner().keyword_strike(self.text)
if bool(self.keyword_strike_dict):
PdfMiner().output_to_csv(self.filename, self.keyword_strike_dict)
def keyword_strike(self, text, keyword_strike_dict={}):
'''keyword_strike function counts how many times a specific keyword occurs'''
self.keyword_strike_dict = {}
self.text = text
self.keywords_list = PdfMiner().extract_keywords()
for keyword in self.keywords_list:
if keyword in text.decode('utf-8'):
self.keyword_strike_dict[keyword] = text.decode('utf-8').count(keyword)
return self.keyword_strike_dict
def extract_keywords(self, keywords_list=None):
'''function extract_keywords extract the keywords from file keywords.txt, into a list'''
keywords_list = []
with open('keywords.txt', 'r', encoding='utf8') as keywords_file:
for keyword in keywords_file:
keywords_list.append(keyword.strip('\n'))
return keywords_list
def extract_text_from_pdf(self, file_destination, text=None):
'''extract_text_from_pdf'''
self.file_destination = file_destination
text = textract.process(self.file_destination, method='tesseract', language='eng', encoding='utf-8')
return text
def output_to_csv(self, *args, **kwargs):
'''output_csv exports results to csv'''
self.filename = args[0]
self.keyword_strike_dict = args[1]
self.output_file_path = PdfMiner().output_path + self.filename.strip('.pdf')
with open(self.output_file_path + '.csv', 'w+', newline='') as csvfile:
row_writer = csv.writer(csvfile, delimiter=',')
row_writer.writerow(['keyword', 'keyword_count'])
for keyword, keyword_count in self.keyword_strike_dict.items():
print(keyword, keyword_count)
row_writer.writerow([keyword, keyword_count])
if __name__ == "__main__":
PdfMiner().main()