Skip to content

Importing tabular data from a pdf

This page shows a way to bring tabular data from a PDF into Excel through Power Query, using Microsoft Word to convert the file first.

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

The method has four parts:

  • Open the PDF in Microsoft Word.
  • Save it from Word as a single file web page.
  • Import that web page file into Power Query as HTML.
  • 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, which matters later:

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.
  • Open Microsoft Word, and open the PDF from within Word.

Word warns that it will convert the PDF to an editable document, which may not look exactly like the original.

  • Tick Don’t show this message again if you wish, and click OK.

Word’s message saying it will convert the PDF to an editable Word document, with a Don’t show this message again box

  • Choose File ➔ Save As, and in the file type list choose Single File Web Page (*.mht, *.mhtml):

Word’s Save As file type list with Single File Web Page highlighted

  • On Excel’s Data tab, choose Get Data ➔ From File ➔ From Text/CSV.

Excel’s Get Data menu open at From File, with From Text/CSV highlighted

  • In the file dialog, change the file type filter from Text Files to All Files, so that the web page file appears, and browse to it.

The file dialog’s type filter changed from Text Files to All Files

  • Select the MHTML file saved earlier and click Import.

The folder holding the Abbreviation-List MHTML document, the original PDF and an Excel workbook

  • Power Query does not recognise the file type, so right-click the file in the dialog that follows and choose HTML.

The right-click menu on the imported file, with HTML highlighted among Excel, Access, Text, CSV, JSON, XML and PDF

Power Query now finds the tables in the document:

The navigation table, listing a Table with the class name 3DTableGrid and a Document

Here the data sits in the table whose class name is 3DTableGrid. In other PDFs it may take some trial and error to find the right table.

  • Click the Table link on the 3DTableGrid row to open that table.

The data is nearly usable at this point, but it carries some leftover formatting from the conversion:

The imported table in four columns, with a header row reading STATE and Abbrev= iation, the states listed below, and rows of leftover span tags near the bottom

  • Remove the header row with Remove Rows ➔ Remove Top Rows, and enter 1.
  • Right-click a cell that holds the text <= /span> and choose Copy, so that you have the exact text.
  • Select all four columns, choose Replace Values on the Transform tab, and replace <= /span> with nothing.
  • The other tables from the PDF now sit in the last 11 rows, which are not needed, so choose Remove Rows ➔ Remove Bottom Rows and enter 11.

The bottom of the table after the span tags are replaced, showing a blank row and then the directional and secondary unit abbreviations that the last 11 rows hold

The data is now in four columns, as two pairs of state and abbreviation, but it belongs in two.

  • In Applied Steps, right-click the last step, choose Rename, and call it All4Columns, so that the next steps can refer to it by name.

The Applied Steps pane with the right-click menu open on Removed Bottom Rows, and Rename highlighted

  • Select the last two columns, right-click, and choose Remove Columns.

The right-click menu on the selected Column3 and Column4, with Remove Columns highlighted

  • Rename the two remaining columns State and Abbreviation.
  • Rename this step First2Columns in the same way.

The State and Abbreviation columns starting at Alabama, with Applied Steps ending in All4Columns, Removed Columns and First2Columns

  • Click the fx button beside the formula bar to add a new step, and replace its formula with the name of the earlier step:
= All4Columns

The new step returns all four columns again, so the second pair can be taken from it:

  • Remove the first two columns.
  • Rename the remaining columns State and Abbreviation, as before.
  • Rename the step Second2Columns.

The query now has two steps that are not next to each other, First2Columns and Second2Columns, and each holds half of the list.

Click the fx button again, and enter this formula to append the two halves into one table:

= Table.Combine({First2Columns, Second2Columns})

The second half has one empty row, beside Missouri in the first half, which a filter on the State column can remove.

The VBA macro below runs in Microsoft Word. It opens a file dialog in which several PDFs can be selected at once, and saves each one as an MHT file beside the original.

Sub ConvertToMHT()
Dim f_dialog As FileDialog
Dim FileChosen As Integer
Dim FileName As String
Dim i As Integer
Set f_dialog = Application.FileDialog(msoFileDialogFilePicker)
f_dialog.InitialView = msoFileDialogViewList
f_dialog.AllowMultiSelect = True
FileChosen = f_dialog.Show
If FileChosen = -1 Then
For i = 1 To f_dialog.SelectedItems.Count
Documents.Open FileName:=f_dialog.SelectedItems(i)
ActiveDocument.SaveAs2 FileName:=Mid(f_dialog.SelectedItems(i), 1, Len(f_dialog.SelectedItems(i)) - 4) & ".mht", FileFormat:=wdFormatWebArchive
ActiveWindow.Close
Next i
End If
End Sub

This example follows Matt Allington’s post Import Tabular Data from PDF using Power Query on Excelerator BI.