Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import re
- import pandas as pd
- import tkinter as tk
- from tkinter import filedialog
- import datetime
- from datetime import date
- import pdfplumber
- today = str(date.today())
- #the dataframe that gets exported to excel, columns will show up in row 1
- exceldf = pd.DataFrame(columns=['Invoice Number', 'Bill or Credit', 'Invoice Date', 'Accounting Date', 'Vendor', 'Invoice Description', 'Line Item Number', 'Property Abbreviation', 'Location', 'GL Account', 'Line Item Description', 'Amount', 'Expense Type', 'Is Replacement Reserve', 'Payment Date', 'Reference Number', 'Payment Method', 'Department', 'Due Date', 'Quantity', 'Unit Price', 'Tax', 'Received Date'])
- text = ""
- x = 1
- y = 1
- #Functions to process the text pulled from the pdf
- def find_invoice_numbers(text):
- #Finds all instances of INVOICE IXXXXXXXXX
- matches = re.findall(r'^I\d{10}', text, flags=re.MULTILINE)
- return list(set(matches))
- def find_account_numbers(text):
- #Finds account number, starts with A and goes 10 digits
- total_pattern = r'A\d{10}'
- matches = re.findall(total_pattern, text, re.MULTILINE)
- return list(dict.fromkeys(matches))
- def find_amount_due(text):
- #Finds all numbers with a decimal point. Will just use the first one found in the je below
- total_pattern = r'\d*\,?\d*\.\d{2}'
- matches = re.findall(total_pattern, text, re.MULTILINE)
- return list(dict.fromkeys(matches))
- def find_invoice_dates(text):
- #Finds all dates and returns no duplicates, will just use the first one on each page
- total_pattern = r'\d{2}\/\d{2}\/\d{4}'
- matches = re.findall(total_pattern, text)
- return list(dict.fromkeys(matches))
- def get_prop_abbr(account_no):
- #uses the account number return property code
- match account_no:
- case 'acct1': return 'prop1'
- case 'acct2': return 'prop2'
- case 'acct3': return 'prop3'
- case 'acct4': return 'prop4'
- case _: return ''
- #opens dialogue box to select the pdf
- pdf_document = filedialog.askopenfilename()
- pdf= pdfplumber.open(pdf_document)
- #writes to dataframe that will then be written to excel
- for page in pdf.pages:
- text = page.extract_text()
- #passes through text to the various functions above
- invoicenum = find_invoice_numbers(text)
- account_num = find_account_numbers(text)
- amount_due = find_amount_due(text)
- invoice_dates = find_invoice_dates(text)
- #using only first result for invoicenum and account_num
- exceldf.loc[y, 'Invoice Number'] = invoicenum[0]
- exceldf.loc[y, 'Bill or Credit'] = 'Bill'
- exceldf.loc[y, 'GL Account'] = 69750
- exceldf.loc[y, 'Invoice Description'] = 'monthly service'
- exceldf.loc[y, 'Line Item Description'] = 'monthly service'
- exceldf.loc[y, 'Invoice Date'] = invoice_dates[0]
- exceldf.loc[y, 'Accounting Date'] = date.today().strftime("%m/%d/%Y")
- exceldf.loc[y, 'Vendor'] = 'VEND1'
- exceldf.loc[y, 'Line Item Number'] = 1
- exceldf.loc[y, 'Property Abbreviation'] = get_prop_abbr(account_num[0])
- #removes any comma and converts it into a float (so it's not a literal)
- exceldf.loc[y, 'Amount'] = float(amount_due[0].replace(",",""))
- exceldf.loc[y, 'Expense Type'] = 'General'
- exceldf.loc[y, 'Is Replacement Reserve'] = 'FALSE'
- y+= 1
- pdf.close()
- #saves excel file to invoiceuploads folder
- exceldf.to_excel('invoiceuploads/vendorupload' + today + '.xlsx', index=False)
- print('A file is now ready to be uploaded in /invoiceuploads.')
Advertisement
Add Comment
Please, Sign In to add comment