Python Automation and Machine Learning for EM and ICs

An Online Book, Second Edition by Dr. Yougui Liao (2019)

Python Automation and Machine Learning for EM and ICs - An Online Book

Chapter/Index: Introduction | A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | Appendix

Automated Excel File Update with and without Hyperlink Addition for Missing Data

The Python script (code) creates an Excel file with a header row formatted to meet specific styling requirements. It uses the openpyxl library to create a new workbook, add a header row with placeholder text, and apply a light green background color, bold text, and thin borders to the cells in the header. The header is designed with the PatternFill, Font, and Border classes from openpyxl.styles to ensure the cells are properly formatted. Finally, the workbook is saved as an .xlsx file. This script is useful for preparing structured Excel files with customized header formats.

The updated script (code) first attempts to load the existing Excel file, header_example.xlsx. If the file is not found, a new one is created. It then checks if the value "Yougui" exists in the first column ('Header1'). If "Yougui" is not found, a new row is appended with "Yougui" and two "make-up" values. The new row is formatted with different background colors (yellow and light blue) for each cell, while maintaining the same thin border style applied to the header. The updated workbook is then saved back to the original file.

Hyperlink can be imported from openpyxl.worksheet.hyperlink:

from openpyxl.worksheet.hyperlink import Hyperlink

Then, code can be used to add Hyperlink to 'Make-up Value 2'.

The column widths can be set by:

ws.column_dimensions['A'].width = 20 # Set width of 'Header1'
ws.column_dimensions['B'].width = 25 # Set width of 'Header2'
ws.column_dimensions['C'].width = 30 # Set width of 'Header3'

 

Background Options

Type Description Example
Solid Fill Solid color fill. #90EE90 (Light green)
Solid Fill Solid color fill. #FFFF00 (Yellow)
Solid Fill Solid color fill. #ADD8E6 (Light blue)
Solid Fill Solid color fill. #ADD8E6 (Blue)
Solid Fill Solid color fill. #FF0000 (Red)
Solid Fill Solid color fill. #FFA500 (Orange)
Solid Fill Solid color fill. #ADD8E6 (Light blue)
Solid Fill Solid color fill. #ADD8E6 (Light blue)
Solid Fill Solid color fill. #800080(Purple)
Gradient Fill Gradient color fill (can be linear or radial). fill_type='gradient'
No Fill No background color. fill_type=None
 

Border Options

Style Description
thin Thin border
medium Medium border
thick Thick border
dotted Dotted border
dashed Dashed border
double Double border
hair Hairline border
mediumDashed Medium dashed border
dashDot Dash-dot border
mediumDashDot Medium dash-dot border
dashDotDot Dash-dot-dot border
mediumDashDotDot Medium dash-dot-dot border