I am new to Python Pandas, need your guidance. I have following below code which extract specific data from a pdf files and export into a excel file. The code is working fine, however all data are exported into text format. Is there any way I can use text and number extract in same code.
I tried using #str.extract(r"([A-Za-z\s]+)([\d-]+)"))- but it did not work. I also tried below link, but could not decipher. Kindly help!!
import os
import pandas as pd
import numpy as np
import glob
import pdfplumber
def get_keyword(start, end, text):
for i in range(len(start)):
try:
field = ((text.split(start[i]))[1].split(end[i])[0])
return field
except:
continue
def main():
my_dataframe = pd.DataFrame()
for files in glob.glob("C:/PDFs\*.pdf"):
with pdfplumber.open(files) as pdf:
page = pdf.pages[0]
text = page.extract_text()
text = " ".join(text.split())
# obtain keyword #1-Find Supplier-This is text & it is fine
start = ['SUPPLIER ']
end = [' Purchase']
keyword1 = get_keyword(start, end, text)
# obtain keyword #2-Find Invoice, This is number-which need to number not text.
start = ['Invoice Weight(Kg) ']
end = ['.00 Net Weight.(Kg)']
keyword2 = get_keyword(start, end, text)
my_list = [keyword1, keyword2]
my_list = pd.Series(my_list)
my_dataframe = my_dataframe.append(my_list, ignore_index=True)
print("Document's keywords have been extracted successfully!")
my_dataframe = my_dataframe.rename(columns={0:'Supplier',
1:'Invoice Number',
2:'Mill Lot Number'})
save_path: str = 'C:/PDFs'
os.chdir(save_path)
# extract my dataframe to an .xlsx file!
my_dataframe.to_excel('sample.xlsx', sheet_name = 'Sheet1')
print("")
print(my_dataframe)
if __name__ == '__main__':
main()