How it works...

In the How to do it… section, in step 1 we import the openpyxl module and create a new Excel file. In step 2, we add the data to the first sheet. Step 3 is also a preparation step to define the colors to be used. The colors are defined in hex format, which is common in the web design world.

To find the definition of colors, there are plenty of color pickers online or even embedded in the OS. A tool like https://coolors.co/ can be useful to define a palette to work with.

In step 4, we prepare the format to define the header. The header will have a different font (Tahoma), a bigger size (14pt), and it will be white on a blue background. To do this, we prepare a Font object with the font, size, and foreground color, and a PatternFill with the background color.

The loop after creating header_font and header_fill applies the font and fill to the proper cells.

Note that iterating over a range always returns the row, then cells, even if only one row is involved.

In step 5, a border to the rows and an alternate background is applied. The border is defined with blue top and bottom and white left and right. The fill is created in a similar way to step 4, but in a light blue. The background is only applied to even rows.

Note that the top border of a cell is the bottom of the one above and vice versa. This means that it's possible to overwrite the border in a loop.

The file is saved finally in step 6.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.181.170