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