3

I have to parse a formatted pdf to get some feilds. The PDF is here. What I need to parse is shown in this imgur. I have used PyPDF2 to get text, But It returns raw text without any formatting.

import PyPDF2
pdfFileObj = open('GPO-PLUMBOOK-2000-4-1.pdf','rb')
pdfReader = PyPDF2.PdfFileReader(pdfFileObj)
pageObj = pdfReader.getPage(0)
print(pageObj.extractText())

Output I got is as follows:

LEGISLATIVE BRANCHLocationPosition TitleName of IncumbentPayPlanType ofAppt.Level,Grade, orPayTenureExpiresARCHITECT OF THE CAPITOLAlan M. HantmanWashington, DCArchitect of the Capitol10 years02/02/07IIIEXPASLocationPosition TitleName of IncumbentPayPlanType ofAppt.Level,Grade, orPayTenureExpiresGENERAL ACCOUNTING OFFICEDavid M. WalkerWashington, DCComptroller General of the United States11/09/1315 years$141,300OTPASVacant  Do...........Deputy Comptroller General of the United States..................OTXSLocationPosition TitleName of IncumbentPayPlanType ofAppt.Level,Grade, orPayTenureExpiresGOVERNMENT PRINTING OFFICEMichael F. DiMarioWashington, DCPublic Printer............IIIEXPASRobert T. Mansker  Do...........Deputy Public Printer............IVEXXSFrancis J. Buckley, Jr.  Do...........Superintendent of Documents..................SLXSRobert G. Andary  Do...........Inspector General..................SLXSMary Beth Lawler  Do...........Staff Assistant............14OTSCLocationPosition TitleName of IncumbentPayPlanType ofAppt.Level,Grade, orPayTenureExpiresLIBRARY OF CONGRESSLIBRARIAN OF CONGRESSJames H. BillingtonWashington, DCLibrarian of Congress............IIIEXPASLIBRARY OF CONGRESS TRUST FUND BOARDJames H. Billington  Do...........Chairman (Ex-Officio)..................WCPASTed Stevens  Do...........Chairman of the Joint Committee of the Library (Ex-Officio)..................WCXSLawrence Summers  Do...........Member (Ex-Officio), Secretary of the Treasury..................WCPASDonald Hammond  Do...........Member (Designee for the Secretary of the Treasurer)..................WCXSCeil Pulitzer  Do...........Member5 years03/23/03......WCPASNajeeb Halaby  Do...........Member5 years08/31/05......WCPASJohn Kluge  Do...........Member5 years03/10/03......WCXSWayne Berman  Do...........Member5 years12/22/01......WCXSEdwin Cox  Do...........Member5 years03/31/04......WCXSJohn Henry  Do...........Member5 years12/22/03......WCXSDonald Jones  Do...........Member5 years10/08/02......WCXSJulie Finley  Do...........Member5 years06/29/01......WCXSBernard Rappaport  Do...........Member5 years12/22/01......WCXS(1)

I need to separate the data e.g Data under Location column and so on.

Alexandre B.
  • 5,387
  • 2
  • 17
  • 40
Ayyan Khan
  • 507
  • 2
  • 12

1 Answers1

0

Take a look at the tabula library (here the github). This returns you a pandas dataframe.

df = tabula.read_pdf("/home/michael/Downloads/GPO-PLUMBOOK-2000-4-1.pdf", pages=1)
df.dropna(inplace=True)
print(df[:2])

You can also adjust which part of the pdf should be used, if you need to read other tables, or want to save time. This way you just read alle the pdf tables in, and slice my data to your wanted output.

mischva11
  • 2,811
  • 3
  • 18
  • 34