mirror of
https://github.com/ksyasuda/dotfiles.git
synced 2026-03-20 18:11:27 -07:00
update skills
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
"""Create a basic spreadsheet with two sheets and a simple formula.
|
||||
|
||||
Usage:
|
||||
python3 create_basic_spreadsheet.py --output /tmp/basic_spreadsheet.xlsx
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
from pathlib import Path
|
||||
|
||||
from openpyxl import Workbook
|
||||
from openpyxl.utils import get_column_letter
|
||||
|
||||
|
||||
def main() -> None:
|
||||
parser = argparse.ArgumentParser(description="Create a basic spreadsheet with example data.")
|
||||
parser.add_argument(
|
||||
"--output",
|
||||
type=Path,
|
||||
default=Path("basic_spreadsheet.xlsx"),
|
||||
help="Output .xlsx path (default: basic_spreadsheet.xlsx)",
|
||||
)
|
||||
args = parser.parse_args()
|
||||
|
||||
wb = Workbook()
|
||||
overview = wb.active
|
||||
overview.title = "Overview"
|
||||
employees = wb.create_sheet("Employees")
|
||||
|
||||
overview["A1"] = "Description"
|
||||
overview["A2"] = "Awesome Company Report"
|
||||
|
||||
employees.append(["Title", "Name", "Address", "Score"])
|
||||
employees.append(["Engineer", "Vicky", "90 50th Street", 98])
|
||||
employees.append(["Manager", "Alex", "500 Market Street", 92])
|
||||
employees.append(["Designer", "Jordan", "200 Pine Street", 88])
|
||||
|
||||
employees["A6"] = "Total Score"
|
||||
employees["D6"] = "=SUM(D2:D4)"
|
||||
|
||||
for col in range(1, 5):
|
||||
employees.column_dimensions[get_column_letter(col)].width = 20
|
||||
|
||||
args.output.parent.mkdir(parents=True, exist_ok=True)
|
||||
wb.save(args.output)
|
||||
print(f"Saved workbook to {args.output}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,96 @@
|
||||
"""Generate a styled games scoreboard workbook using openpyxl.
|
||||
|
||||
Usage:
|
||||
python3 create_spreadsheet_with_styling.py --output /tmp/GamesSimpleStyling.xlsx
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
from pathlib import Path
|
||||
|
||||
from openpyxl import Workbook
|
||||
from openpyxl.formatting.rule import FormulaRule
|
||||
from openpyxl.styles import Alignment, Font, PatternFill
|
||||
from openpyxl.utils import get_column_letter
|
||||
|
||||
HEADER_FILL_HEX = "B7E1CD"
|
||||
HIGHLIGHT_FILL_HEX = "FFF2CC"
|
||||
|
||||
|
||||
def apply_header_style(cell, fill_hex: str) -> None:
|
||||
cell.fill = PatternFill("solid", fgColor=fill_hex)
|
||||
cell.font = Font(bold=True)
|
||||
cell.alignment = Alignment(horizontal="center", vertical="center")
|
||||
|
||||
|
||||
def apply_highlight_style(cell, fill_hex: str) -> None:
|
||||
cell.fill = PatternFill("solid", fgColor=fill_hex)
|
||||
cell.font = Font(bold=True)
|
||||
cell.alignment = Alignment(horizontal="center", vertical="center")
|
||||
|
||||
|
||||
def populate_game_sheet(ws) -> None:
|
||||
ws.title = "GameX"
|
||||
ws.row_dimensions[2].height = 24
|
||||
|
||||
widths = {"B": 18, "C": 14, "D": 14, "E": 14, "F": 40}
|
||||
for col, width in widths.items():
|
||||
ws.column_dimensions[col].width = width
|
||||
|
||||
headers = ["", "Name", "Game 1 Score", "Game 2 Score", "Total Score", "Notes", ""]
|
||||
for idx, value in enumerate(headers, start=1):
|
||||
cell = ws.cell(row=2, column=idx, value=value)
|
||||
if value:
|
||||
apply_header_style(cell, HEADER_FILL_HEX)
|
||||
|
||||
players = [
|
||||
("Vicky", 12, 30, "Dominated the minigames."),
|
||||
("Yash", 20, 10, "Emily main with strong defense."),
|
||||
("Bobby", 1000, 1030, "Numbers look suspiciously high."),
|
||||
]
|
||||
for row_idx, (name, g1, g2, note) in enumerate(players, start=3):
|
||||
ws.cell(row=row_idx, column=2, value=name)
|
||||
ws.cell(row=row_idx, column=3, value=g1)
|
||||
ws.cell(row=row_idx, column=4, value=g2)
|
||||
ws.cell(row=row_idx, column=5, value=f"=SUM(C{row_idx}:D{row_idx})")
|
||||
ws.cell(row=row_idx, column=6, value=note)
|
||||
|
||||
ws.cell(row=7, column=2, value="Winner")
|
||||
ws.cell(row=7, column=3, value="=INDEX(B3:B5, MATCH(MAX(E3:E5), E3:E5, 0))")
|
||||
ws.cell(row=7, column=5, value="Congrats!")
|
||||
|
||||
ws.merge_cells("C7:D7")
|
||||
for col in range(2, 6):
|
||||
apply_highlight_style(ws.cell(row=7, column=col), HIGHLIGHT_FILL_HEX)
|
||||
|
||||
rule = FormulaRule(formula=["LEN(A2)>0"], fill=PatternFill("solid", fgColor=HEADER_FILL_HEX))
|
||||
ws.conditional_formatting.add("A2:G2", rule)
|
||||
|
||||
|
||||
def main() -> None:
|
||||
parser = argparse.ArgumentParser(description="Create a styled games scoreboard workbook.")
|
||||
parser.add_argument(
|
||||
"--output",
|
||||
type=Path,
|
||||
default=Path("GamesSimpleStyling.xlsx"),
|
||||
help="Output .xlsx path (default: GamesSimpleStyling.xlsx)",
|
||||
)
|
||||
args = parser.parse_args()
|
||||
|
||||
wb = Workbook()
|
||||
ws = wb.active
|
||||
populate_game_sheet(ws)
|
||||
|
||||
for col in range(1, 8):
|
||||
col_letter = get_column_letter(col)
|
||||
if col_letter not in ws.column_dimensions:
|
||||
ws.column_dimensions[col_letter].width = 12
|
||||
|
||||
args.output.parent.mkdir(parents=True, exist_ok=True)
|
||||
wb.save(args.output)
|
||||
print(f"Saved workbook to {args.output}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,59 @@
|
||||
"""Read an existing .xlsx and print a small summary.
|
||||
|
||||
If --input is not provided, this script creates a tiny sample workbook in /tmp
|
||||
and reads that instead.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
|
||||
from openpyxl import Workbook, load_workbook
|
||||
|
||||
|
||||
def create_sample(path: Path) -> Path:
|
||||
wb = Workbook()
|
||||
ws = wb.active
|
||||
ws.title = "Sample"
|
||||
ws.append(["Item", "Qty", "Price"])
|
||||
ws.append(["Apples", 3, 1.25])
|
||||
ws.append(["Oranges", 2, 0.95])
|
||||
ws.append(["Bananas", 5, 0.75])
|
||||
ws["D1"] = "Total"
|
||||
ws["D2"] = "=B2*C2"
|
||||
ws["D3"] = "=B3*C3"
|
||||
ws["D4"] = "=B4*C4"
|
||||
wb.save(path)
|
||||
return path
|
||||
|
||||
|
||||
def main() -> None:
|
||||
parser = argparse.ArgumentParser(description="Read an existing spreadsheet.")
|
||||
parser.add_argument("--input", type=Path, help="Path to an .xlsx file")
|
||||
args = parser.parse_args()
|
||||
|
||||
if args.input:
|
||||
input_path = args.input
|
||||
else:
|
||||
tmp_dir = Path(tempfile.gettempdir())
|
||||
input_path = tmp_dir / "sample_read_existing.xlsx"
|
||||
create_sample(input_path)
|
||||
|
||||
wb = load_workbook(input_path, data_only=False)
|
||||
print(f"Loaded: {input_path}")
|
||||
print("Sheet names:", wb.sheetnames)
|
||||
|
||||
for name in wb.sheetnames:
|
||||
ws = wb[name]
|
||||
max_row = ws.max_row or 0
|
||||
max_col = ws.max_column or 0
|
||||
print(f"\n== {name} (rows: {max_row}, cols: {max_col})")
|
||||
for row in ws.iter_rows(min_row=1, max_row=min(max_row, 5), max_col=min(max_col, 5)):
|
||||
values = [cell.value for cell in row]
|
||||
print(values)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,79 @@
|
||||
"""Create a styled spreadsheet with headers, borders, and a total row.
|
||||
|
||||
Usage:
|
||||
python3 styling_spreadsheet.py --output /tmp/styling_spreadsheet.xlsx
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
from pathlib import Path
|
||||
|
||||
from openpyxl import Workbook
|
||||
from openpyxl.styles import Alignment, Border, Font, PatternFill, Side
|
||||
|
||||
|
||||
def main() -> None:
|
||||
parser = argparse.ArgumentParser(description="Create a styled spreadsheet example.")
|
||||
parser.add_argument(
|
||||
"--output",
|
||||
type=Path,
|
||||
default=Path("styling_spreadsheet.xlsx"),
|
||||
help="Output .xlsx path (default: styling_spreadsheet.xlsx)",
|
||||
)
|
||||
args = parser.parse_args()
|
||||
|
||||
wb = Workbook()
|
||||
ws = wb.active
|
||||
ws.title = "FirstGame"
|
||||
|
||||
ws.merge_cells("B2:E2")
|
||||
ws["B2"] = "Name | Game 1 Score | Game 2 Score | Total Score"
|
||||
|
||||
header_fill = PatternFill("solid", fgColor="B7E1CD")
|
||||
header_font = Font(bold=True)
|
||||
header_alignment = Alignment(horizontal="center", vertical="center")
|
||||
ws["B2"].fill = header_fill
|
||||
ws["B2"].font = header_font
|
||||
ws["B2"].alignment = header_alignment
|
||||
|
||||
ws["B3"] = "Vicky"
|
||||
ws["C3"] = 50
|
||||
ws["D3"] = 60
|
||||
ws["E3"] = "=C3+D3"
|
||||
|
||||
ws["B4"] = "John"
|
||||
ws["C4"] = 40
|
||||
ws["D4"] = 50
|
||||
ws["E4"] = "=C4+D4"
|
||||
|
||||
ws["B5"] = "Jane"
|
||||
ws["C5"] = 30
|
||||
ws["D5"] = 40
|
||||
ws["E5"] = "=C5+D5"
|
||||
|
||||
ws["B6"] = "Jim"
|
||||
ws["C6"] = 20
|
||||
ws["D6"] = 30
|
||||
ws["E6"] = "=C6+D6"
|
||||
|
||||
ws.merge_cells("B9:E9")
|
||||
ws["B9"] = "=SUM(E3:E6)"
|
||||
|
||||
thin = Side(style="thin")
|
||||
border = Border(top=thin, bottom=thin, left=thin, right=thin)
|
||||
ws["B9"].border = border
|
||||
ws["B9"].alignment = Alignment(horizontal="center")
|
||||
ws["B9"].font = Font(bold=True)
|
||||
|
||||
for col in ("B", "C", "D", "E"):
|
||||
ws.column_dimensions[col].width = 18
|
||||
ws.row_dimensions[2].height = 24
|
||||
|
||||
args.output.parent.mkdir(parents=True, exist_ok=True)
|
||||
wb.save(args.output)
|
||||
print(f"Saved workbook to {args.output}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user