Selection: Formatting#
For certain formats (notably xls and xlsx) selecting based on cell format is also supported.
The following are example methods:
is_bold / is_not_bold: Does the cell have bold formatting.is_indented / is_not_indented: Does the cell have indented text.is_italic / is_not_italic: Does the cell have italic formatting.is_underline / is_not_underline: Does the cell have underline formatting.is_hyperlink / is_hyperlinkIs the cell a hyperlink.
We’ll use the following sample file as a simple example:
from tidychef import acquire, preview
# Acquire the XLSX file
table = acquire.xlsx.http("https://raw.githubusercontent.com/mikeAdamss/tidychef/main/tests/fixtures/xlsx/simple-formatting.xlsx", tables="Sheet1")
preview(table)
Sheet1
| A | B | C | D | E | F | |
| 1 | ||||||
| 2 | ||||||
| 3 | I am bold! | |||||
| 4 | itallic text | |||||
| 5 | ||||||
| 6 | www.google.com | |||||
| 7 | Underlined text | |||||
| 8 | ||||||
| 9 | ||||||
| 10 | no formatting | |||||
| 11 | no formatting | indett 1 | ||||
| 12 | indent 2 |
Now we’ll make some simple conditional selections via the format based methods.
bold_cells = table.is_bold().label_as("Bold")
italic_cells = table.is_italic().label_as("Italic")
underlined_cells = table.is_underline().label_as("Underlined")
hyperlink_cells = table.is_hyperlink().label_as("Hypterlinked")
indented_cells = table.is_indented().label_as("Indented")
preview(bold_cells, italic_cells, underlined_cells, hyperlink_cells, indented_cells)
| Bold |
| Italic |
| Underlined |
| Hypterlinked |
| Indented |
Sheet1
| A | B | C | D | E | F | |
| 1 | ||||||
| 2 | ||||||
| 3 | I am bold! | |||||
| 4 | itallic text | |||||
| 5 | ||||||
| 6 | www.google.com | |||||
| 7 | Underlined text | |||||
| 8 | ||||||
| 9 | ||||||
| 10 | no formatting | |||||
| 11 | no formatting | indett 1 | ||||
| 12 | indent 2 |
Key Points - Underline vs Hyperlink#
By default we treat cells formatted as underlined and hypterlinked as distinct even though there is obvious one side overlap (all hyperlinks are underlined but not all underlined cells are hyperlinks).
It’s worth noting this is default not mandated behaviour as shown by the following examples:
Example 1: Treat as distinct formatting (default behaviour)#
underlined = table.is_underline().label_as("Underlined")
preview(underlined)
| Underlined |
Sheet1
| A | B | C | D | E | F | |
| 1 | ||||||
| 2 | ||||||
| 3 | I am bold! | |||||
| 4 | itallic text | |||||
| 5 | ||||||
| 6 | www.google.com | |||||
| 7 | Underlined text | |||||
| 8 | ||||||
| 9 | ||||||
| 10 | no formatting | |||||
| 11 | no formatting | indett 1 | ||||
| 12 | indent 2 |
Example 2: Treat both simple underlined and hypterlinks as underlined#
Note the use of a keyword argument here.
underlined = table.is_underline(include_hyperlinks=True).label_as("Underlined")
preview(underlined)
| Underlined |
Sheet1
| A | B | C | D | E | F | |
| 1 | ||||||
| 2 | ||||||
| 3 | I am bold! | |||||
| 4 | itallic text | |||||
| 5 | ||||||
| 6 | www.google.com | |||||
| 7 | Underlined text | |||||
| 8 | ||||||
| 9 | ||||||
| 10 | no formatting | |||||
| 11 | no formatting | indett 1 | ||||
| 12 | indent 2 |