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.

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.
1. The example file
Section titled “1. The example file”The example uses a PDF that lists the US states and their two-letter abbreviations:
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:

- Save a copy of the PDF somewhere you can find it.
2. Convert the PDF to a web page file
Section titled “2. Convert the PDF to a web page file”- 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.

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

3. Import the file into Power Query
Section titled “3. Import the file into Power Query”- On Excel’s Data tab, choose Get Data ➔ From File ➔ From Text/CSV.

- 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.

- Select the MHTML file saved earlier and click Import.

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

Power Query now finds the tables in the 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.
4. Clean the data
Section titled “4. Clean the data”The data is nearly usable at this point, but it carries some leftover formatting from the conversion:

- 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.

5. Split the four columns into two pairs
Section titled “5. Split the four columns into two pairs”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.

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

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

- Click the fx button beside the formula bar to add a new step, and replace its formula with the name of the earlier step:
= All4ColumnsThe 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.
6. Append the two halves
Section titled “6. Append the two halves”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.
7. Convert several PDFs with VBA
Section titled “7. Convert several PDFs with VBA”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 FileDialogDim FileChosen As IntegerDim FileName As StringDim i As IntegerSet f_dialog = Application.FileDialog(msoFileDialogFilePicker)f_dialog.InitialView = msoFileDialogViewListf_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 IfEnd SubThis example follows Matt Allington’s post Import Tabular Data from PDF using Power Query on Excelerator BI.