Skip to content

Instantly share code, notes, and snippets.

@pranjalAI
Last active June 29, 2021 13:33
Show Gist options
  • Save pranjalAI/c57756f34f2c87c8f1ee499852c4a5ce to your computer and use it in GitHub Desktop.
Save pranjalAI/c57756f34f2c87c8f1ee499852c4a5ce to your computer and use it in GitHub Desktop.
with pdfplumber.open("VISTA_Menu_Authorizations.pdf") as pdf:
next_page_continue=False
for pcount in range(len(pdf.pages)):
page=pdf.pages[pcount]
text=page.extract_text()
if not next_page_continue:
out=[]
next_page_menu=[]
#getting code & description
menu_re= re.compile(r'^MENU:')
for line in text.split("\n"):
if(menu_re.match(line)):
new_menu_re=[]
for i in line.split(" "):
if i=="":
pass
else:
new_menu_re.append(i.strip())
code=new_menu_re[1]
desc=new_menu_re[2]
print(code, desc)
#getting menu details
menu_func_re=re.compile(r'^\s*\d+\s{2}[A-Za-z\s\d\W]+$')
menu_dict={}
for line in text.split("\n"):
if(menu_func_re.match(line)):
if(line.split(" ")[1].split(" ")[0]=="CALL"):
pass
else:
line_split=line.strip().split(" ")
for l in line_split:
key,*value=l.strip().split(" ")
value=" ".join(value)
menu_dict[key]=value.strip()
menu_index=list(menu_dict.keys())
menu_index=[i for i in menu_index if i.isnumeric()]
print(menu_index)
#APM thing
APM_re=re.compile(r'\s*\d+\s+APM')
APM_re_internal=re.compile(r"^AUTHORIZED:")
auth_dict={}
for line in text.split("\n"):
if(APM_re.match(line)):
menu_number=line.strip().split(" ")[0]
if(APM_re_internal.match(line)):
auth_title,*auth=line.split(" ")
auth=[i for i in auth if i!=""]
if(menu_number in list(auth_dict.keys())):
auth_dict[str(int(menu_number))].append(auth)
else:
auth_dict[str(int(menu_number))]=auth
#making table
for menu_ind in menu_index:
if menu_ind in list(auth_dict.keys()):
tokens=auth_dict[menu_ind]
for tok in tokens:
li=[menu_ind,code,desc,menu_dict[menu_ind],tok]
out.append(li)
else:
next_page_menu.append(menu_ind)
next_page_menu_to_use=next_page_menu
if(len(next_page_menu)>0):
next_page_continue=True
else:
next_page_continue=False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment