---
title: Iterating over tables in an existing Excel workbook with OpenPyXL
description: Iterating over tables in an existing Excel workbook with OpenPyXL
---

[Conny Söderholm](https://www.connysoderholm.com/blog)

# [Iterating over tables in an existing Excel workbook with OpenPyXL](https://www.connysoderholm.com/blog/iterating-over-tables-in-an-existing-excel-workbook-with-openpyxl)

 Written by [Conny Söderholm](https://www.connysoderholm.com/blog/author/conny-soderholm) | July 31, 2023

There is a way to iterate over Excel tables with OpenPyXL. The functionality has changed in newer versions of OpenPyXL. The iterator is accessed from the `worksheets tables` property. 

First we load the workbook and set the active sheet:

`from openpyxl import load_workbook`

`FILENAME = "Tables.xlsx"`

`wb = load_workbook(FILENAME)`

`ws = wb.active`

 

In order to access all tables, we can do a for loop of the worksheet.tables.values(). After that we can get the attributes of the table, for example, the name with table.name and the tables reference with table.ref

When we have the Worksheet object ws we can then iterate over the tables.

`for table in ws.tables.values():`

`    print(table.name, table.ref)`

 

## The Complete Code

Source code is available on my [GitHub](https://github.com/consod/articles/tree/main/OpenPyXL/tables/Resize%20tables)

```
#!/usr/bin/env python3

"""
Iterating over tables in an existing workbook
"""
from openpyxl import load_workbook

FILENAME = "Tables.xlsx"

wb = load_workbook(FILENAME)

ws = wb.active

for table in ws.tables.values():
    print(table.name, table.ref)
```

 

[View full post](https://www.connysoderholm.com/blog/iterating-over-tables-in-an-existing-excel-workbook-with-openpyxl)

```json
{
  "@context" : "http://schema.org",
  "@type" : "BlogPosting",
  "author" : {
    "@type" : "Person",
    "name" : "Conny Söderholm"
  },
  "dateModified" : "2023-08-01T03:50:09.250Z",
  "datePublished" : "2023-07-31T18:59:17Z",
  "headline" : "Iterating over tables in an existing Excel workbook with OpenPyXL",
  "image" : {
    "@type" : "ImageObject",
    "height" : 175,
    "url" : "https://f.hubspotusercontent-eu1.net/hubfs/141158987/Table.png",
    "width" : 300
  },
  "mainEntityOfPage" : "https://www.connysoderholm.com/blog/iterating-over-tables-in-an-existing-excel-workbook-with-openpyxl",
  "publisher" : {
    "@type" : "Organization",
    "logo" : {
      "@type" : "ImageObject",
      "height" : 60,
      "url" : "/hs/hsstatic/content_shared_assets/static-1.4092/img/default-amp-logo.png",
      "width" : 60
    },
    "name" : "Conny Söderholm"
  }
}
```