Dimora to xlsx (4)
ぶるれこ様の
データを
なるべく
手間を かけないで!
xlsxで 読み込めるようにする
以前も 作ったけど・・・
ぜんめんりにゅ~ある である
製作:Microsoft Copilot様
OnLineBackUp で ありんす♪
# ==== xlsx to openpyxl to ods 変!
from openpyxl import load_workbook
from openpyxl.styles import Font, PatternFill, Border, Side, Alignment
from openpyxl.worksheet.page import PageMargins
# === 定数 ===
INPUT_FILE = r"F:\_pypy\DIGA\COPY\recList-USB.xlsx"
OUTPUT_FILE = r"F:\_pypy\DIGA\COPY\recList-USB-styled.xlsx"
# Excelファイルを読み込み
wb = load_workbook(INPUT_FILE)
ws = wb.active
# --- 列幅を辞書形式で指定 ---
col_widths = {
"A": 17,
"B": 12,
"C": 66,
"D": 5,
"E": 4,
"F": 8,
"G": 6,
"H": 10,
"I": 3,
"J": 6,
}
for col, width in col_widths.items():
ws.column_dimensions[col].width = width
# --- タイトル行のスタイル ---
title_font = Font(name="00コミック7", size=10, bold=True, color="000000")
title_fill = PatternFill(start_color="FFFF99", end_color="FFFF99", fill_type="solid") # 薄い黄色
thin_border = Border(
left=Side(style="thin"),
right=Side(style="thin"),
top=Side(style="thin"),
bottom=Side(style="thin")
)
for cell in ws[1]: # 1行目(タイトル行)
cell.font = title_font
cell.fill = title_fill
cell.border = thin_border
cell.alignment = Alignment(horizontal="center", vertical="center")
# --- データ行のフォント指定(列ごとに異なる) ---
for row in ws.iter_rows(min_row=2, max_row=ws.max_row):
row[0].font = Font(name="Times New Roman", size=12, bold=True) # 列A
row[1].font = Font(name="Times New Roman", size=12, bold=True) # 列B
row[2].font = Font(name="木漏れ日ゴシック", size=12, bold=True) # 列C
row[3].font = Font(name="Times New Roman", size=12, bold=True) # 列D
row[4].font = Font(name="Times New Roman", size=12, bold=True) # 列E
row[5].font = Font(name="Times New Roman", size=12, bold=True) # 列F
row[6].font = Font(name="Times New Roman", size=12, bold=True) # 列G
row[7].font = Font(name="Times New Roman", size=10, bold=False) # 列H
row[8].font = Font(name="Times New Roman", size=6, bold=False) # 列I
row[9].font = Font(name="Times New Roman", size=6, bold=False) # 列J
# 罫線を全セルに適用
for cell in row:
cell.border = thin_border
# --- セルの書式設定例 ---
for row in ws.iter_rows(min_row=2, max_row=ws.max_row):
# 分(整数+3桁区切り)
row[3].number_format = "#,##0"
# 放送開始日時(h:mm)
row[1].number_format = "h:mm"
# 通貨列がある場合(例: 列J)
# row[9].number_format = "¥#,##0"
# --- 印刷設定 ---
ws.page_setup.paperSize = ws.PAPERSIZE_A4
ws.page_setup.orientation = ws.ORIENTATION_LANDSCAPE
ws.page_margins = PageMargins(left=0.25, right=0.25, top=0.25, bottom=0.25)
# 保存
wb.save(OUTPUT_FILE)
print(f"整形完了: {OUTPUT_FILE}")
## ✅ このコードのポイント
# - **列幅**:辞書形式で管理 → `col_widths` に列と幅をまとめて指定
# - **タイトル行**:フォント「00コミック7」、背景色「薄い黄色」、罫線+中央揃え
# - **データ行**:列ごとにフォントを変える(Times New Roman、木漏れ日ゴシックなど)
# - **セル書式**:数値は `#,##0`、時間は `h:mm`、通貨は `¥#,##0`
# - **印刷設定**:A4横向き、余白最小
# これで「見た目だけ整える」処理が一通り可能になります。
# 👉 次のステップとしては「条件付き書式(例:未視聴なら背景色を変える)」を追加することもできますが、
# まずはこの基本スタイルで試してみますか?
# # ==============================