【PowerShell】PowerPintのプロパティのタイトルを一括で変更する
PowerPointやExcelなどのOffice製品では、プロパティでタイトルを設定することができます。
PowerPointだけを使っている場合にはあまり意識しなくても良い部分ですが、PDFにエクスポートする時ファイルのタイトルとしてタブに表示されます。
ファイル名と同じにしておくと無難ですが、ファイルがたくさんあると一つ一つ変更するのは面倒です。
大量にあるPowerPointのファイルのタイトルをファイル名に変更してPDFとして出力するPowerShellのスクリプトです。
試行錯誤した履歴も含めて。
# 最初は再帰でやろうとしたけど失敗。無限ループになる
# Function titleRename($dir) {
# foreach($file in $dir) {
# if ($file.Mode.Substring(0,1) -eq "d") {
# check $file
# } ElseIf($file.Name.Substring($file.Name.Length - 5, 5) -eq ".pptx") {
# $ppt = New-Object -ComObject PowerPoint.Application
# $pres = $ppt.Presentations.Open($file.FullName)
# $pres.BuiltInDocumentProperties("title") = $pres.Name.substring(0, $pres.Name.length - 5)
# $pres.Save()
# $pres.Close()
# $ppt.Quit()
# }
# }
# }
# $dir = Get-ChildItem
# titleRename $dir
# -Recurseで取得するようにしたらうまくいった
$dir = Get-ChildItem -Recurse
$ppt = New-Object -ComObject PowerPoint.Application
foreach($file in $dir) {
# 拡張子が.pptxのファイルを対象にする
If (($file.Name.Length -gt 5) -And ($file.Name.Substring($file.Name.Length - 5, 5) -eq ".pptx")) {
$pres = $ppt.Presentations.Open($file.FullName)
# プロパティのタイトルをファイル名に変更
$pres.BuiltInDocumentProperties("title") = $pres.Name.substring(0, $pres.Name.length - 5)
$pres.Save()
# PDFに出力する
$pres.SaveAs($file.FullName.Substring(0, $file.FullName.Length - 5), 32)
$pres.Close()
}
}
$ppt.Quit()
# 上で方法でもできたけどもうちょっと最適化
# フィルターをかけてForEach-Objectにしてみた
$ppt = New-Object -ComObject PowerPoint.Application
Get-ChildItem -Recurse -Filter *.pptx | ForEach-Object {
$pres = $ppt.Presentations.Open($_.FullName)
# プロパティのタイトルをファイル名に変更
$pres.BuiltInDocumentProperties("title") = $pres.Name.substring(0, $pres.Name.length - 5)
$pres.Save()
# PDFに出力する 第二引数はファイルタイプ。PDFは32
$pres.SaveAs($_.FullName.Substring(0, $_.FullName.Length - 5), 32)
$pres.Close()
}
$ppt.Quit()