AffectionateKey7126

Invoice processing

Dec 30th, 2024 (edited)
1,274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.47 KB | None | 0 0
  1. import re
  2. import pandas as pd
  3. import tkinter as tk
  4. from tkinter import filedialog
  5. import datetime
  6. from datetime import date
  7. import pdfplumber
  8.  
  9. today = str(date.today())
  10.  
  11. #the dataframe that gets exported to excel, columns will show up in row 1
  12. 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'])
  13.  
  14. text = ""
  15. x = 1
  16. y = 1
  17.  
  18. #Functions to process the text pulled from the pdf
  19. def find_invoice_numbers(text):
  20.     #Finds all instances of INVOICE IXXXXXXXXX
  21.     matches = re.findall(r'^I\d{10}', text, flags=re.MULTILINE)
  22.     return list(set(matches))
  23.  
  24. def find_account_numbers(text):
  25.     #Finds account number, starts with A and goes 10 digits
  26.     total_pattern = r'A\d{10}'
  27.     matches = re.findall(total_pattern, text, re.MULTILINE)
  28.     return list(dict.fromkeys(matches))
  29.  
  30. def find_amount_due(text):
  31.     #Finds all numbers with a decimal point. Will just use the first one found in the je below
  32.     total_pattern = r'\d*\,?\d*\.\d{2}'
  33.     matches = re.findall(total_pattern, text, re.MULTILINE)
  34.     return list(dict.fromkeys(matches))
  35.  
  36. def find_invoice_dates(text):
  37.     #Finds all dates and returns no duplicates, will just use the first one on each page
  38.     total_pattern = r'\d{2}\/\d{2}\/\d{4}'
  39.     matches = re.findall(total_pattern, text)
  40.     return list(dict.fromkeys(matches))
  41.  
  42. def get_prop_abbr(account_no):
  43.     #uses the account number return property code
  44.     match account_no:
  45.             case 'acct1': return 'prop1'
  46.             case 'acct2': return 'prop2'
  47.             case 'acct3': return 'prop3'
  48.             case 'acct4': return 'prop4'
  49.             case _: return ''
  50.  
  51. #opens dialogue box to select the pdf
  52. pdf_document = filedialog.askopenfilename()
  53. pdf= pdfplumber.open(pdf_document)
  54.  
  55. #writes to dataframe that will then be written to excel
  56. for page in pdf.pages:
  57.     text = page.extract_text()
  58.  
  59.     #passes through text to the various functions above
  60.     invoicenum = find_invoice_numbers(text)
  61.     account_num = find_account_numbers(text)
  62.     amount_due = find_amount_due(text)
  63.     invoice_dates = find_invoice_dates(text)
  64.  
  65.     #using only first result for invoicenum and account_num
  66.     exceldf.loc[y, 'Invoice Number'] = invoicenum[0]
  67.     exceldf.loc[y, 'Bill or Credit'] = 'Bill'
  68.     exceldf.loc[y, 'GL Account'] = 69750
  69.     exceldf.loc[y, 'Invoice Description'] = 'monthly service'
  70.     exceldf.loc[y, 'Line Item Description'] = 'monthly service'
  71.     exceldf.loc[y, 'Invoice Date'] = invoice_dates[0]
  72.     exceldf.loc[y, 'Accounting Date'] = date.today().strftime("%m/%d/%Y")
  73.     exceldf.loc[y, 'Vendor'] = 'VEND1'
  74.     exceldf.loc[y, 'Line Item Number'] = 1
  75.     exceldf.loc[y, 'Property Abbreviation'] = get_prop_abbr(account_num[0])
  76.  
  77.     #removes any comma and converts it into a float (so it's not a literal)
  78.     exceldf.loc[y, 'Amount'] = float(amount_due[0].replace(",",""))
  79.     exceldf.loc[y, 'Expense Type'] = 'General'
  80.     exceldf.loc[y, 'Is Replacement Reserve'] = 'FALSE'
  81.     y+= 1
  82.  
  83.    
  84. pdf.close()    
  85. #saves excel file to invoiceuploads folder
  86.    
  87. exceldf.to_excel('invoiceuploads/vendorupload' + today + '.xlsx', index=False)
  88. print('A file is now ready to be uploaded in /invoiceuploads.')
  89.  
Tags: python
Advertisement
Add Comment
Please, Sign In to add comment