Use Python to Stylize the Excel Formatting
By: Yeung Wong
Tags: Formatting, Automation, Excel, Programming, Python

Excel Formatting

Finally, it comes to the main dish. Just remember one thing, before writing the code, you should have a design in mind first. For example, below is my draft template. The reason to have the draft is because it helps you better define the column or index to put the elements (title, table, remark, etc).

Photo by cyda

1. Add the table to the excel

file_name = "Cryptocurrency.xlsx"

sheet_name = "Summary" writer = pd.ExcelWriter(file_name, engine='xlsxwriter')

master_df.to_excel(writer, sheet_name=sheet_name, startrow = 2, index = False)

Just one point to note, you may see that there is a parameter startrow set to 2. It is because we would like to leave the first two rows for the title and the spacing and we will start writing the table in row 3 (Python count value starting from 0 so basically setting as 2 means the 3rd row)

Photo by cyda

2. Add the title to the excel

from datetime import datetime

workbook = writer.book

worksheet = writer.sheets[sheet_name] worksheet.write(0, 0, 'Cryptocurrency Pricing Summary on '+datetime.now().strftime('%d %b %Y'), workbook.add_format({'bold': True, 'color': '#E26B0A', 'size': 14}))

To define which cell to write the text, you can count from top to bottom then from left to right. For example, if you are writing A1, then it is (0, 0). If you are writing C4, then it should be (3, 2).

Photo by cyda

3. Add the remark to the excel

worksheet.write(len(master_df)+4, 0, 'Remark:', workbook.add_format({'bold': True}))

worksheet.write(len(master_df)+5, 0, 'The last update time is ' + datetime.now().strftime('%H:%M') + '.')

Here is one trick to bear in mind. Since the data frame may have a different number of rows in every update, you should better consider its records when writing the remark string. For example, here I set the row index as len(master_df)+4 which is the number of rows in the table plus the title and spacings.

Photo by cyda

4. Add the color to the table header

header_format = workbook.add_format({'bold': True, 'text_wrap': True, 'fg_color': '#FDE9D9', 'border': 1})

for col_num, value in enumerate(master_df.columns.values):

worksheet.write(2, col_num, value, header_format)

For the table headers, two parameters are introduced. The first one is text_wrap. It is set to True so that the text for the header will be wrapped to the next line if the space is not enough to display. The second one is fg_color. It is used to set the foreground color of the cell.

Photo by cyda

5. Add the border to the table

row_idx, col_idx = master_df.shape

for r in range(row_idx):

for c in range(col_idx):

if c == 6:

worksheet.write(r + 3, c, master_df.values[r, c], workbook.add_format({'border': 1, 'num_format': '0.00%'}))

else:

worksheet.write(r + 3, c, master_df.values[r, c], workbook.add_format({'border': 1, 'num_format': '0.00'}))

The trick is similar to previous cases. Just remember to set the row index to be r+3 because there are three rows ahead of the first value in the table.

Photo by cyda

6. Set the Column Width

worksheet.set_column(0, 6, 12)

worksheet.set_column(1, 1, 20)

For your reference, the function parameter is as follow:

(start column index, end column index, column width)