Skip to content

Instantly share code, notes, and snippets.

@eyasuyuki
Created June 5, 2026 05:05
Show Gist options
  • Select an option

  • Save eyasuyuki/be6da028bc2cb4fa69b5916e2f611067 to your computer and use it in GitHub Desktop.

Select an option

Save eyasuyuki/be6da028bc2cb4fa69b5916e2f611067 to your computer and use it in GitHub Desktop.
Excelに画像貼り付け
# ==============================================================================
# 設定エリア(カレントディレクトリを基準に設定)
# ==============================================================================
# スクリプトが実行された現在の場所(カレントディレクトリ)をルートフォルダにする
$rootFolder = Get-Location
# 出力するExcelファイルのパス(カレントディレクトリに「Result.xlsx」として保存)
$excelPath = Join-Path $rootFolder "Result.xlsx"
# ==============================================================================
# メイン処理
# ==============================================================================
Write-Host "処理を開始します..."
Write-Host "読み込み元: $rootFolder"
Write-Host "出力先パス: $excelPath"
# Excelオブジェクトの起動
$excel = New-Object -ComObject Excel.Application
$excel.Visible = $false
$excel.DisplayAlerts = $false
# 新規ブックの作成
$workbook = $excel.Workbooks.Add()
# 初期状態のシート数を1つにする
while ($workbook.Sheets.Count -gt 1) {
$workbook.Sheets.Item($workbook.Sheets.Count).Delete()
}
$isFirstSheet = $true
# ルート直下のフォルダ(=シート名)を取得
$sheetFolders = Get-ChildItem -Path $rootFolder | Where-Object { $_.PSIsContainer }
foreach ($sheetFolder in $sheetFolders) {
$sheetName = $sheetFolder.Name
# 1番目のシートは既存のシートの名前を変更、2番目以降は新規追加
if ($isFirstSheet) {
$sheet = $workbook.Sheets.Item(1)
$sheet.Name = $sheetName
$isFirstSheet = $false
} else {
$sheet = $workbook.Sheets.Add([System.Reflection.Missing]::Value, $workbook.Sheets.Item($workbook.Sheets.Count))
$sheet.Name = $sheetName
}
Write-Host "シートを作成中: $sheetName"
# A列とB列の見出し
$sheet.Cells.Item(1, 1).Value = "キャプション"
$sheet.Cells.Item(1, 2).Value = "画像"
$sheet.Rows.Item(1).Font.Bold = $true
# 現在の書き込み行(2行目からスタート)
$currentRow = 2
# シート名フォルダ配下のフォルダ(=キャプション名)を取得
$captionFolders = Get-ChildItem -Path $sheetFolder.FullName | Where-Object { $_.PSIsContainer }
foreach ($captionFolder in $captionFolders) {
$captionName = $captionFolder.Name
# キャプションフォルダ内の画像ファイルを取得 (jpg, jpeg, png, bmp)
$images = Get-ChildItem -Path $captionFolder.FullName | Where-Object {
$_.Extension -match '(?i)\.(jpg|jpeg|png|bmp)'
}
if ($images.Count -eq 0) { continue }
# A列にキャプション名を記入
$sheet.Cells.Item($currentRow, 1).Value = $captionName
# 複数画像をB列に縦に並べて貼り付け
foreach ($image in $images) {
$imagePath = $image.FullName
# 貼り付け先のセル位置(B列)を取得
$targetCell = $sheet.Cells.Item($currentRow, 2)
try {
# 1. 一旦、位置決定のために画像を仮挿入
$shape = $sheet.Pictures().Insert($imagePath)
$left = $targetCell.Left
$top = $targetCell.Top
# 原寸大(100%)を維持するための設定
$shape.ShapeRange.ScaleWidth(1.0, [Microsoft.Office.Core.MsoTriState]::msoTrue)
$shape.ShapeRange.ScaleHeight(1.0, [Microsoft.Office.Core.MsoTriState]::msoTrue)
$width = $shape.Width
$height = $shape.Height
# 仮挿入した画像を削除
$shape.Delete()
# 2. 完全な埋め込み画像として再挿入
$finalShape = $sheet.Shapes.AddPicture($imagePath, [Microsoft.Office.Core.MsoTriState]::msoFalse, [Microsoft.Office.Core.MsoTriState]::msoTrue, $left, $top, $width, $height)
# 画像の高さに合わせて、Excelの行の高さを自動調整
$rowHeight = $height + 5
if ($rowHeight -gt 409.5) { $rowHeight = 409.5 }
$sheet.Rows.Item($currentRow).RowHeight = $rowHeight
# 画像が重ならないよう、次の画像は下の行へ
$currentRow++
}
catch {
Write-Warning "画像の挿入に失敗しました: $imagePath"
}
}
}
# A列(キャプション列)の幅を自動調整
$sheet.Columns.Item(1).AutoFit()
}
# ファイルの保存処理
if (Test-Path $excelPath) {
Remove-Item $excelPath
}
$workbook.SaveAs($excelPath)
# 後片付け
$workbook.Close()
$excel.Quit()
[System.Runtime.InteropServices.Marshal]::ReleaseComObject($excel) | Out-Null
Remove-Variable excel
Write-Host "すべての処理が完了しました!" -ForegroundColor Green
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment