Skip to content

Instantly share code, notes, and snippets.

@officialcjunior
Created July 31, 2024 05:26
Show Gist options
  • Save officialcjunior/4beb866cd673958f5177cffff898ca3b to your computer and use it in GitHub Desktop.
Save officialcjunior/4beb866cd673958f5177cffff898ca3b to your computer and use it in GitHub Desktop.
A hacker's guide to Openpyxl - a Python library for interacting with Excel
  1. Loading a workbook:
wb = load_workbook(filename = pruned_bugs_path, data_only=False)

sheet = wb['sheetname']

Apart from filename, there are some parameters such as data_only, read_only, keep_vba, data_only, keep_links.

  1. Creating a table:
# Creating a table for the outstanding bugs
from openpyxl.worksheet.table import Table, TableStyleInfo
table_range = f'A4:T'+ str(sheet.max_row + 3)
tab = Table(displayName='outstandingbugs', ref=table_range)
style = TableStyleInfo(name='TableStyleLight12', showFirstColumn=False, showLastColumn=False, showRowStripes=True, showColumnStripes=True)
tab.tableStyleInfo = style
report_sheet_outstanding_bugs.add_table(tab)
  1. Writing everything in a cell in red color.
  cell = header['C5']
  cell.font = Font(name='Arial', color=Color('FF0000'), bold=True, size=14)  # Use RGB hex code for color
  cell.value = current_version
  1. Conditional formatting
red_fill = PatternFill(bgColor="FF0000")
dxf = DifferentialStyle(font=black_text, fill=red_fill)
rule = Rule(text="S1-System stop", type='containsText', operator="containsText", dxf=dxf)
rule.formula = ['NOT(ISERROR(SEARCH("S1-System stop",B5)))']
report_sheet_outstanding_bugs.conditional_formatting.add('B5:B1000', rule)
high_visibility_bugs.conditional_formatting.add('B5:B1000', rule)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment