Skip to content

Importing tabular data from a pdf - new

Newer versions of Excel and Power BI have a PDF connector, which reads tables straight out of a PDF document. This page uses it to bring tabular data from a PDF into Excel, without the Word conversion that older versions needed.

A PDF icon, an arrow to a Power Query icon, and an arrow to an Excel icon

The method has two parts:

  • Import the PDF into Power Query with the PDF connector.
  • Clean and reshape the data in Power Query as usual.

The example uses a PDF that lists the US states and their two-letter abbreviations:

The example PDF

The top of its first page looks like this. The table is laid out as two pairs of columns side by side:

The top of the PDF, headed Standard 2-Letter Abbreviations, with STATE and Abbreviation columns repeated twice across the page

  • Save a copy of the PDF somewhere you can find it.
  • On Excel’s Data tab, choose Get Data ➔ From File ➔ From PDF.

Excel’s Get Data menu open at From File, with From PDF highlighted

Browse to the PDF and open it. Power Query reads the file and opens the Navigator, which lists each table it found and each page of the document. Here it finds one table on page 1, Table001, and one page, Page001.

The Navigator listing Table001 (Page 1) and Page001, with a preview of Table001 in four columns

Click an item to preview it, or tick Select multiple items to pick several. Load sends the selection straight to Excel, while Transform Data opens it in the Power Query Editor for cleaning first.

Choose Table001 and click Transform Data. The table opens in the editor in four columns, with the headings in the first row:

Table001 in the Power Query Editor, with the STATE and Abbreviation headings in row 1 and the states in two pairs of columns

  • In Applied Steps, rename the last step, which is usually Changed Type, to All4Columns, so that later steps can refer to it by name.
  • Select the first two columns, which are the ones to keep, then right-click and choose Remove Other Columns:

The right-click menu on the selected Column1 and Column2, with Remove Other Columns highlighted

  • On the Transform tab, click Use First Row as Headers.
  • Filter the STATE column to remove blank rows and the row holding the heading DIRECTIONAL ABBREVIATIONS. The resulting step looks like this:
= Table.SelectRows(#"Promoted Headers", each ([STATE] <> "" and [STATE] <> "DIRECTIONAL#(lf)ABBREVIATIONS"))

The #(lf) in the text stands for the line break that splits the heading over two lines in the PDF.

  • Rename this step First2Columns.
  • Click the fx button beside the formula bar to add a new step, and enter this formula, which goes back to the All4Columns step and keeps the other two columns:
= Table.SelectColumns(All4Columns,{"Column3", "Column4"})
  • Click Use First Row as Headers again.
  • Filter the STATE column to remove blank rows and the row holding the heading SECONDARY ADDRESS UNIT INDICATORS:
= Table.SelectRows(#"Promoted Headers1", each ([STATE] <> "" and [STATE] <> "SECONDARY ADDRESS#(lf)UNIT INDICATORS"))
  • Rename this step Second2Columns.
  • Click the fx button again, and enter this formula to append the two halves into one table:
= Table.Combine({First2Columns, Second2Columns})
  • Rename the step AllData.

Loaded to Excel, the result is a single list of states and abbreviations:

The finished Excel table with STATE and Abbreviation columns, starting at Alabama

The whole query, as shown in the Advanced Editor, looks like the code below. The file path in the first step will be different on your computer.

let
Source = Pdf.Tables(File.Contents("C:\Users\...Abbreviation-List.pdf"), [Implementation="1.2"]),
Table001 = Source{[Id="Table001"]}[Data],
All4Columns = Table.TransformColumnTypes(Table001,{{"Column1", type text}, {"Column2", type text}, {"Column3", type text}, {"Column4", type text}}),
#"Select Columns" = Table.SelectColumns(All4Columns,{"Column1", "Column2"}),
#"Promoted Headers" = Table.PromoteHeaders(#"Select Columns", [PromoteAllScalars=true]),
First2Columns = Table.SelectRows(#"Promoted Headers", each ([STATE] <> "" and [STATE] <> "DIRECTIONAL#(lf)ABBREVIATIONS")),
#"Select Columns1" = Table.SelectColumns(All4Columns,{"Column3", "Column4"}),
#"Promoted Headers1" = Table.PromoteHeaders(#"Select Columns1", [PromoteAllScalars=true]),
Second2Columns = Table.SelectRows(#"Promoted Headers1", each ([STATE] <> "" and [STATE] <> "SECONDARY ADDRESS#(lf)UNIT INDICATORS")),
AllData = Table.Combine({First2Columns, Second2Columns})
in
AllData

Pdf.Tables also takes optional settings in its second argument. Two of them, StartPage and EndPage, limit the import to a range of pages, which helps with a long document. Edit the Source step’s formula to add them, as in this example:

Pdf.Tables(File.Contents("C:\Sample.pdf"), [StartPage=5, EndPage=10])

The Pdf.Tables reference lists the other settings.

The PDF used on this page comes from Matt Allington’s post Import Tabular Data from PDF using Power Query on Excelerator BI.