Created
January 19, 2011 16:05
-
-
Save oxoofo/786366 to your computer and use it in GitHub Desktop.
Word,Excel,PowerPointをPDFに一括変換
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!ruby -Ks | |
# -*- coding: utf-8 -*- | |
require "win32ole" | |
require "fileutils" | |
def usage | |
puts <<-USAGE.gsub(/^ *\|/, "") | |
|動作: ワード・エクセル・PowerPointをPDFに一括変換します。 | |
| ネストした階層構造をそのまま再現します。 | |
| 変換元にPDFが有れば、そのままコピーします。 | |
| | |
|使用方法: exeの場合 「>dxp2pdf INPUT_DIR OUTPUT_DIR」 | |
| rubyの場合「>ruby dxp2pdf.rb INPUT_DIR OUTPUT_DIR」 | |
| | |
|INPUT_DIR: 入力ディレクトリを指定します。 | |
| *.doc, *.xls, *.ppt, *pdf を含むディレクトリです。 | |
| 階層構造も可(サブディレクトリも対象)です。 | |
| | |
|OIUPUT_DIR: 出力ディレクトリを指定します。 | |
| ディレクトリは、無ければ作ります。 | |
| ファイルが既存なら、上書きせず、変換をスキップします。 | |
USAGE | |
# > 「exeの場合」 | |
# exerbでexeに。 | |
end | |
@logfile = open("dxp2pdf.log", "w") | |
def puts(*args) | |
STDOUT.puts(*args) | |
@logfile.puts(*args) | |
end | |
def print(*args) | |
STDOUT.print(*args) | |
@logfile.print(*args) | |
end | |
def main | |
if ARGV.length == 2 | |
in_dir,out_dir = ARGV | |
else | |
usage | |
return | |
end | |
puts Time.now.strftime("%Y-%m-%d %H:%M:%S") | |
word = WIN32OLE.new("Word.Application") | |
excel = WIN32OLE.new("Excel.Application") | |
ppt = WIN32OLE.new("PowerPoint.Application") | |
begin | |
excel_exts = ['.xls', '.xlsx', '.xlw'] | |
exts = excel_exts + ['.doc', '.ppt', '.pdf'] | |
files = Dir["#{in_dir}/**/[^~]*{#{exts.join(',')}}"] # 「.../[^~]...」 Wordの一時ファイルを除外 | |
skip = complete = failure = 0 | |
rest = files.size | |
files.sort.each_with_index do |file, i| | |
src_fullpath = File.expand_path(file) | |
ext = File.extname(src_fullpath) | |
dst_fullpath = src_fullpath.sub(in_dir, out_dir).sub(/\.[^.]+$/, ".pdf") | |
FileUtils.mkdir_p(File.dirname(dst_fullpath)) | |
puts "■ #{i+1} / #{files.size}" | |
puts "入力: #{src_fullpath}" | |
puts "出力: #{dst_fullpath}" | |
if File.exist?(dst_fullpath) | |
print "スキップ(出力ファイルが既存)" | |
skip += 1 | |
else | |
case ext.downcase | |
when '.doc' | |
print "変換中... " | |
begin | |
#doc = word.Documents.Open(src_fullpath.gsub('/', '\\')) | |
# 「修正個所の表示」ダイアログを表示させない | |
# (ファイルの状態が不正で自動修正が行われたときに出るダイアログ) | |
doc = word.Documents.OpenNoRepairDialog(src_fullpath.gsub('/', '\\')) | |
doc.ExportAsFixedFormat({ "OutputFileName" => dst_fullpath.gsub('/', '\\'), | |
"ExportFormat" => 17, | |
"OpenAfterExport" => false }) | |
print "完了" | |
complete += 1 | |
rescue => e | |
print "失敗: #{e}" | |
failure += 1 | |
ensure | |
doc.Close if doc | |
end | |
when *excel_exts | |
print "変換中... " | |
begin | |
book = excel.Workbooks.Open(src_fullpath.gsub('/', '\\')) | |
book.ExportAsFixedFormat({ "FileName" => dst_fullpath.gsub('/', '\\'), | |
"Type" => 0, | |
"OpenAfterPublish" => false }) | |
print "完了" | |
complete += 1 | |
rescue => e | |
print "失敗: #{e}" | |
failure += 1 | |
ensure | |
if book | |
#保存確認ダイアログを表示させない(黙って保存せず終了する) | |
#(セルの再計算などが起きた場合に出る) | |
book.Saved = true | |
book.Close | |
end | |
end | |
when '.ppt' | |
print "変換中... " | |
begin | |
presen = ppt.Presentations.Open(src_fullpath.gsub('/', '\\')) | |
#presen.ExportAsFixedFormat({ "Path" => dst_fullpath.gsub('/', '\\'), | |
# "FixedFormatType" => 2, | |
# }) | |
presen.SaveAs(dst_fullpath, 32) | |
print "完了" | |
complete += 1 | |
rescue => e | |
print "失敗: #{e}" | |
failure += 1 | |
ensure | |
presen.Close if presen | |
end | |
when '.pdf' | |
print "コピー... " | |
begin | |
FileUtils.cp(src_fullpath, dst_fullpath) | |
print "完了" | |
complete += 1 | |
rescue => e | |
print "失敗: #{e}" | |
failure += 1 | |
end | |
end | |
end | |
rest -= 1 | |
puts " (スキップ:#{skip} 完了:#{complete} 失敗:#{failure} 残:#{rest}})" | |
puts | |
end | |
ensure | |
word.Quit | |
excel.Quit | |
ppt.Quit | |
end | |
puts Time.now.strftime("%Y-%m-%d %H:%M:%S") | |
@logfile.close | |
end | |
main() | |
# 参考: | |
# | |
# WordファイルやExcelファイルを一括でPDFに変換するRubyスクリプト - looselife diary | |
# http://d.hatena.ne.jp/kinumi/20100209/1265726880 | |
# | |
# ↑参考というより、 | |
# ・Dirで *.doc や *.xls のループ | |
# ・OLEでWordやExcelのPDF保存機能(office2007アドオン)を呼び出す | |
# と、処理の中核の部分は、この記事そのままです。 | |
# この gist で追加してるのは、「サブディレクトリも再帰したい」とか、 | |
# 今日、自分のやった作業の場合に必要だった、こまごましたカスタマイズのみです。 | |
# | |
# | |
# [Windows PowerShell] PowerPoint プレゼンテーションを XPS, PDF に保存する - Shigeya Tanabe's blog - Site Home - TechNet Blogs | |
# http://blogs.technet.com/b/stanabe/archive/2008/12/10/windows-powershell-powerpoint-xps-pdf.aspx | |
# | |
# ↑PowerPoint では、Presentation#ExportAsFixedFormat() じゃなくて Presentaion#SaveAs()を使う | |
# 動作環境: | |
# windows/ruby1.8.6(one click installer) | |
# ms office 2007 + PDFへ保存するMS製アドオン(http://www.microsoft.com/downloads/details.aspx?FamilyID=4d951911-3e7e-4ae6-b059-a2e79ed87041&DisplayLang=ja) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment