Skip to content

GitLab

  • Projects
  • Groups
  • Snippets
  • Help
    • Loading...
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
O
openpyxl
  • Project overview
    • Project overview
    • Details
    • Activity
    • Releases
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 200
    • Issues 200
    • List
    • Boards
    • Labels
    • Service Desk
    • Milestones
  • Merge Requests 2
    • Merge Requests 2
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Operations
    • Operations
    • Incidents
    • Environments
  • Packages & Registries
    • Packages & Registries
    • Container Registry
  • Analytics
    • Analytics
    • CI/CD
    • Repository
    • Value Stream
  • Snippets
    • Snippets
  • Members
    • Members
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • openpyxl
  • openpyxl
  • Issues
  • #1596

Closed
Open
Created Jan 26, 2021 by Jean-Francois Thuong@jfthuong

Enhancement: documentation on Tables is a bit limited

Documentation on Tables is currently quite limited, as it only indicates:

  • How to create a Table
  • How to get a Table object
  • How to delete a Table

There is nothing on how to use a Table, like for example:

  • Getting the range of the table (i.e. tab.ref)
  • Looping over lines / column values
  • Adding lines to the table
  • Expanding table to new cells (e.g. tab.ref = "A1:D10")
  • Modifying column names

We could add this to "Working with Tables"


Getting the range of a Table
++++++++++++++++++++++++++++

.. code::

  >>> tab.ref
  "A1:D10"

Expanding the range of a Table
++++++++++++++++++++++++++++++

For example, if the range is "A1:D10" and we need to extand to 10 lines, we would do:
.. code::

  >>> tab.ref = "A1:D20"

Adding lines to a Table
+++++++++++++++++++++++

This consists is setting values in new cells, and expanding the range of the Table (see above).

.. code::

  from openpyxl.utils import range_boundaries, get_column_letter
  
  data = [
      ['Kiwis', 4000, 3000, 2000, 2000],
      ['Lemons', 3000, 2000, 4000, 5000],
  ]
  
  coord = list(range_boundaries(tb.ref))
  
  for row in data:
      coord[-1] += 1
      ws.append(row)
  
  tab.ref = (
      f"{get_column_letter(coord[0])}{coord[1]}:"
      f"{get_column_letter(coord[2])}{coord[3]}"
  )

For the iteration of lines, I have tried a small function stored in SO: https://stackoverflow.com/a/65895166/1603480

Assignee
Assign to
None
Milestone
None
Assign milestone
Time tracking
None
Due date
None