Acrobat python

[Python]PDFのクリエーター情報の削除

20240327013756_1508x458

【スクリプトエディタで開く】|

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
# メタデータを削除します
# pythonを利用します
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.6"
use framework "Foundation"
use framework "PDFKit"
use framework "Quartz"
use scripting additions


property refMe : a reference to current application

set appFileManager to refMe's NSFileManager's defaultManager()

set ocidFalse to (refMe's NSNumber's numberWithBool:false)
set ocidTrue to (refMe's NSNumber's numberWithBool:true)

####何が前面か
tell application "System Events"
  set strAppTitile to title of (front process whose frontmost is true)
end tell

set listAliasFilePath to {}
###前面がプレビュー
if strAppTitile is "プレビュー" then
  tell application "Preview"
    tell front document
      set strFilePath to path as text
      set boolMode to modified
      ###変更箇所があるなら保存する
      if boolMode is true then
close saving yes
      else
close saving no
      end if
    end tell
  end tell
  set aliasFilePath to POSIX file strFilePath
  copy aliasFilePath to end of listAliasFilePath
  ###前面がリーダー
else if strAppTitile is "Acrobat Reader" then
  tell application id "com.adobe.Reader"
activate
    ##ファイルパス
    tell active doc
      set aliasFilePath to file alias
    end tell
  end tell
  set strFilePath to (POSIX path of aliasFilePath) as text
  tell application id "com.adobe.Reader"
activate
    set objAvtivDoc to active doc
    tell objAvtivDoc
      set boolMode to modified
      ###変更箇所があるなら保存する
      if boolMode is true then
save
      end if
    end tell
close objAvtivDoc
  end tell
  copy aliasFilePath to end of listAliasFilePath
  ###それ以外ならダイアログ
else
  ##############################
  #####ダイアログを前面に出す
  ##############################
  tell current application
    set strName to name as text
  end tell
  ####スクリプトメニューから実行したら
  if strName is "osascript" then
    tell application "Finder"
activate
    end tell
  else
    tell current application
activate
    end tell
  end if
  #######################################
  #####ファイル選択ダイアログ
  #######################################
  ###ダイアログのデフォルト
  set ocidUserDesktopPath to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask))
  set aliasDefaultLocation to ocidUserDesktopPath as alias
  tell application "Finder"
  end tell
  set listChooseFileUTI to {"com.adobe.pdf"}
  set strPromptText to "PDFファイルを選んでください" as text
  set listAliasFilePath to (choose file with prompt strPromptText default location (aliasDefaultLocation) of type listChooseFileUTI with invisibles and multiple selections allowed without showing package contents) as list
end if


#######################################
###ダイアログで選択した書類の数だけ繰り返し
#######################################
repeat with itemAliasFilePath in listAliasFilePath
  set strFilePath to POSIX path of itemAliasFilePath as text
  set ocidFilePathStr to (refMe's NSString's stringWithString:strFilePath)
  set ocidFilePath to ocidFilePathStr's stringByStandardizingPath
  set strFileName to ocidFilePath's lastPathComponent() as text
  set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:ocidFilePath isDirectory:false)
  #####PDFDocumentとして読み込み
  set ocidActivDoc to (refMe's PDFDocument's alloc()'s initWithURL:ocidFilePathURL)
  ########################################
  #####暗号化チェック
  set boolEncrypted to ocidActivDoc's isEncrypted()
  if boolEncrypted is true then
    set strMes to "エラー:" & strFileName & "暗号化されています" as text
display alert strMes buttons {"OK", "キャンセル"} default button "OK" as informational giving up after 3
return "暗号化されているので変更できません"
  end if
  ########################################
  #####ロック確認
  set boolLocked to ocidActivDoc's isLocked()
  if boolLocked is true then
    set strMes to "エラー:" & strFileName & "パスワードでロックされています" as text
display alert strMes buttons {"OK", "キャンセル"} default button "OK" as informational giving up after 3
return "パスワードでロックされているので変更できません"
  end if
  ########################################
  ###Pythonイストールチェック
  
  set listReturn to doChkPython3()
  set boolDone to (item 1 of listReturn) as boolean
  set strBinPath to (item 2 of listReturn) as text
  
  if boolDone is false then
    set strMes to "エラー:Pythonパッケージのインストールに失敗しました" as text
display alert strMes buttons {"OK", "キャンセル"} default button "OK" as informational giving up after 3
return "エラー:Pythonパッケージのインストールに失敗しました"
    
  else if strBinPath is ("missing value") then
    set strMes to "エラー:Pythonがインストールされていません" as text
display alert strMes buttons {"OK", "キャンセル"} default button "OK" as informational giving up after 3
return "Pythonがインストールされていません"
  end if
  ########################################
  #Pyファイル保存先
  set ocidTempDirURL to appFileManager's temporaryDirectory()
  set ocidUUID to refMe's NSUUID's alloc()'s init()
  set ocidUUIDString to ocidUUID's UUIDString
  set ocidSaveDirPathURL to (ocidTempDirURL's URLByAppendingPathComponent:(ocidUUIDString) isDirectory:true)
  set ocidAttrDict to (refMe's NSMutableDictionary's alloc()'s initWithCapacity:0)
(ocidAttrDict's setValue:(511) forKey:(refMe's NSFilePosixPermissions))
  set listBoolMakeDir to (appFileManager's createDirectoryAtURL:(ocidSaveDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error|:(reference))
  ###パス
  set strFileName to "Context.py" as text
  set ocidSaveFilePathURL to (ocidSaveDirPathURL's URLByAppendingPathComponent:(strFileName) isDirectory:false)
  #Pyファイルを生成
  set strPyString to ("#!/usr/bin/env python3\n# coding: utf-8\nimport sys\nfrom PyPDF2 import PdfReader, PdfWriter\nfile_path = sys.argv[1]\nreader = PdfReader(file_path)\nwriter = PdfWriter()\nfor page in reader.pages:\n writer.add_page(page)\nwriter.add_metadata({\"/Producer\": \"\", \"/Creator\": \"\",})\nwith open(file_path, \"wb\") as f:\n writer.write(f)\n") as text
  set ocidPyString to (refMe's NSString's stringWithString:(strPyString))
  #Pyファイル保存
  set listDone to (ocidPyString's writeToURL:(ocidSaveFilePathURL) atomically:(true) encoding:(refMe's NSUTF8StringEncoding) |error|:(reference))
  #Pyファイルアクセス権
  set ocidFilePath to ocidSaveFilePathURL's |path|
  set listDone to (appFileManager's setAttributes:(ocidAttrDict) ofItemAtPath:(ocidFilePath) |error|:(reference))
  ##コマンド整形
  set strPyPath to ocidFilePath as text
  set strCommandText to ("\"" & strBinPath & "\" \"" & strPyPath & "\" \"" & strFilePath & "\"") as text
  #コマンド実行
  set strResponse to (do shell script strCommandText) as text
  
  ####解放
  set ocidActivDoc to ""
  set ocidDocAttrDict to ""
end repeat
#########################
###出来上がったPDFを開く
#########################
if strAppTitile is "プレビュー" then
  tell application "Preview"
open (ocidFilePathURL as alias)
  end tell
else if strAppTitile is "Acrobat Reader" then
  tell application id "com.adobe.Reader"
activate
open (ocidFilePathURL as alias)
  end tell
else
  tell application "Preview"
activate
open (ocidFilePathURL as alias)
  end tell
  
end if
return "処理終了"

####################
## python3のチェック
####################
to doChkPython3()
  ## python3のパス
  try
    set strCmdText to ("/usr/bin/which python3") as text
    set strBinPath to (do shell script strCmdText) as text
  on error
log "python3が見つかりませんでした終了します"
return {false, (missing value)} as list
  end try
  ###PyPDF2のインストールチェック
  set strCmdText to ("\"" & strBinPath & "\" -m pip show PyPDF2") as text
  try
    set strPipDone to (do shell script strCmdText) as text
  on error
    try
      set strCmdText to ("/bin/mkdir -p $HOME/Library/Caches/pip") as text
do shell script strCmdText
      set strCmdText to ("/bin/chmod 777 $HOME/Library/Caches/pip") as text
do shell script strCmdText
      set strCmdText to ("/bin/mkdir -p $HOME/Library/Python/3.9/lib/python/site-packages") as text
do shell script strCmdText
      set strCmdText to ("/bin/chmod 755 $HOME/Library/Python/3.9/lib/python/site-packages") as text
do shell script strCmdText
    end try
    try
      ##pipのアップデート
      set strCmdText to ("\"" & strBinPath & "\" -m pip install --upgrade --user pip") as text
      set strPipDone to (do shell script strCmdText) as text
    on error
log "pipのアップデートに失敗しました"
return {false, (missing value)} as list
    end try
    try
      ##インストールされていなければインストールする
      set strCmdText to ("\"" & strBinPath & "\" -m pip install --user PyPDF2") as text
      set strPipDone to (do shell script strCmdText) as text
    on error
log "PyPDF2のインストールに失敗しました"
return {false, (missing value)} as list
    end try
  end try
return {true, strBinPath} as list
end doChkPython3


|

[python3]PDF2UP回転考慮 上下開き 縮小 CROP対応

ダウンロード - PDF2UP上下縮小.zip




【スクリプトエディタで開く】

#!/usr/bin/env python3
import sys
import math
from pypdf import PdfReader
from pypdf import PdfWriter
from pypdf import PageObject
from pypdf import Transformation
from pypdf.generic import RectangleObject

#########################################入力ファイル受け取り
argGetData = sys.argv
strFilePath = str(argGetData[1])

###テスト用
##strFilePath = "/path/to/pdf.pdf"

strOutputPDFDocPath = strFilePath + ".2in1.pdf"
try:
  with open(strFilePath, 'rb') as objInputData:
#########################################ライター初期化
    objReader = PdfReader(objInputData)
    objWriter = PdfWriter()
#########################################ページ数調べる
    objAllPages = objReader.pages
    numAllPage = int(len(objAllPages))
    print(numAllPage)
    numRepeatTImes = math.ceil(numAllPage / 2 )
    print(numRepeatTImes)
#########################################ページ数2回数繰り返し
    numCntInPage = int(0)
    for numTimes in range(numRepeatTImes):
########################################右ページ取り出し
      objPageR = objReader.pages[numCntInPage]
      numCropXR = objPageR.cropbox.left
      numCropYR = objPageR.cropbox.bottom
      numCropWR = objPageR.cropbox.right
      numCropHR = objPageR.cropbox.top
      print("numCropXR:" , numCropXR)
      print("numCropYR:" , numCropYR)
      print("numCropWR:" , numCropWR)
      print("numCropHR:" , numCropHR)
      rectCropBoxR = objPageR.cropbox
      objPageR.mediabox = RectangleObject((numCropXR,numCropYR,numCropWR,numCropHR))
      objPageR.cropbox = RectangleObject((numCropXR,numCropYR,numCropWR,numCropHR))
      objPageR.trimbox = RectangleObject((numCropXR,numCropYR,numCropWR,numCropHR))
      objPageR.bleedbox = RectangleObject((numCropXR,numCropYR,numCropWR,numCropHR))
      objPageR.artbox = rectCropBoxR

      numRotationR = objPageR.rotation
      print("Rotation:" , numRotationR)
      if (numRotationR == 0) | (numRotationR == 180) :
        numWR = numCropWR - numCropXR
        numHR = numCropHR - numCropYR
      elif (numRotationR == 90) | (numRotationR == 270) :
        numWR = numCropHR - numCropYR
        numHR = numCropWR - numCropXR
      print("R :" , numWR , "x" , numHR)
#########################################奇数ページの場合
      if numCntInPage == (numAllPage - 1):
        objPageL = PageObject.create_blank_page(width=numWR, height=numHR)
        numWL = numWR
        numHL = numHR
      else:
        numCntInPage = numCntInPage + 1
########################################左ページ取り出し
        objPageL = objReader.pages[numCntInPage]
        numCropXL = objPageL.cropbox.left
        numCropYL = objPageL.cropbox.bottom
        numCropWL = objPageL.cropbox.right
        numCropHL = objPageL.cropbox.top
        print("numCropXL:" , numCropXL)
        print("numCropYL:" , numCropYL)
        print("numCropWL:" , numCropWL)
        print("numCropHL:" , numCropHL)
        rectCropBoxL = objPageL.cropbox
        objPageL.mediabox = RectangleObject((numCropXL,numCropYL,numCropWL,numCropHL))
        objPageL.cropbox = RectangleObject((numCropXL,numCropYL,numCropWL,numCropHL))
        objPageL.trimbox = RectangleObject((numCropXL,numCropYL,numCropWL,numCropHL))
        objPageL.bleedbox = RectangleObject((numCropXL,numCropYL,numCropWL,numCropHL))
        objPageL.artbox = rectCropBoxL

        numRotationL = objPageL.rotation
        print("Rotation:" , numRotationL)
        if (numRotationL == 0) | (numRotationL == 180) :
          numWL = numCropWL - numCropXL
          numHL = numCropHL - numCropYL
        elif (numRotationL == 90) | (numRotationL == 270) :
          numWL = numCropHL - numCropYL
          numHL = numCropWL - numCropXL
        print("L :" , numWL , "x" , numHL)
        numCntInPage = numCntInPage + 1
      #sys.exit(0)  
#########################################新規ページの高
        numNewH = numHR + numHL
#########################################新規ページの幅は最大値
      if numWR >= numWL:
        numNewW = numWR
      else:
        numNewW = numWL
#########################################上下サイズで新規ページ
      print("2W:" , numNewW , "x" , numNewH)
      objNewPage =  PageObject.create_blank_page(None,numNewW,numNewH)
#########################################下側ページを新規ページに
      if (numRotationL == 0):
        numNewRotationL = 0
        numTXL = 0 - numCropXL
        numTYL = 0 - numCropYL
      elif (numRotationL == 90):
        numNewRotationL = 270
        numTXL = 0 - numCropYR
        numTYL = numHL + numCropXL
      elif (numRotationL == 180):
        numNewRotationL = 180
        numTXL = numWL + numCropXL
        numTYL = numHL + numCropYL
      elif (numRotationL == 270):
        numNewRotationL = 90
        numTXL = numWL +  numCropYL
        numTYL = 0 - numCropXL
      objOption = Transformation().rotate(numNewRotationL).translate(tx=numTXL, ty=numTYL)
      objPageL.add_transformation(objOption,True)

      objPageL.mediabox = RectangleObject((0,0,numWL,numHL))
      objPageL.cropbox = RectangleObject((0,0,numWL,numHL))
      objPageL.trimbox = RectangleObject((0,0,numWL,numHL))
      objPageL.bleedbox = RectangleObject((0,0,numWL,numHL))
      objPageL.artbox = RectangleObject((0,0,numWL,numHL))

      objNewPage.merge_page(objPageL,False)
#########################################上側ページのリサイズ
      if (numRotationR == 0):
        numNewRotationR = 0
        numTXR = 0 - numCropXR
        numTYR = numHR - numCropYR
      elif (numRotationR == 90):
        numNewRotationR = 270
        numTXR = 0 - numCropYR
        numTYR = numHR + numHL  + numCropXR
      elif (numRotationR == 180):
        numNewRotationR = 180
        numTXR = numWR + numCropXR
        numTYR = numHR + numHL + numCropYR
      elif (numRotationR == 270):
        numNewRotationR = 90
        numTXR = numWR + numCropYR
        numTYR = numHL - numCropXR
      objOption = Transformation().rotate(numNewRotationR).translate(tx=numTXR, ty=numTYR)
      objPageR.add_transformation(objOption,True)
      objPageR.mediabox = RectangleObject((0,numHL,numWR,numNewH))
      objPageR.cropbox = RectangleObject((0,numHL,numWR,numNewH))
      objPageR.trimbox = RectangleObject((0,numHL,numWR,numNewH))
      objPageR.bleedbox = RectangleObject((0,numHL,numWR,numNewH))
      objPageR.artbox = RectangleObject((0,numHL,numWR,numNewH))
      objNewPage.merge_page(objPageR,True)
#########################################新規ページのサイズ
      objNewPage.mediabox = RectangleObject((0,0,numNewW,numNewH))
      objNewPage.cropbox = RectangleObject((0,0,numNewW,numNewH))
      objNewPage.trimbox = RectangleObject((0,0,numNewW,numNewH))
      objNewPage.bleedbox = RectangleObject((0,0,numNewW,numNewH))
      objNewPage.artbox = RectangleObject((0,0,numNewW,numNewH))

########################################objNewPageを縮小する
      if numNewW >= (numNewH / 2):
        ratioScaleW = numNewH / 2
        ratioScaleH = numNewW
        objNewPage.scale_to(ratioScaleW,ratioScaleH)
      else:
        ratioScaleW = numNewW / 2
        ratioScaleH = numNewH / 2
        objNewPage.scale_to(ratioScaleW,ratioScaleH)
#########################################縮小後のページサイズ
      objNewPage.mediabox = RectangleObject((0,0,ratioScaleW,ratioScaleH))
      objNewPage.cropbox = RectangleObject((0,0,ratioScaleW,ratioScaleH))
      objNewPage.trimbox = RectangleObject((0,0,ratioScaleW,ratioScaleH))
      objNewPage.bleedbox = RectangleObject((0,0,ratioScaleW,ratioScaleH))
      objNewPage.artbox = RectangleObject((0,0,ratioScaleW,ratioScaleH))

      objWriter.add_page(objNewPage)
except FileNotFoundError:
  print("ファイルのオープンに失敗しました")
  sys.exit(0
try:
#########################################
  with open(strOutputPDFDocPath, 'wb') as objOutPutData:
    objWriter.write(objOutPutData)
except FileNotFoundError:
    print("ファイルの保存に失敗しました")

sys.exit(0


|

[python3]PDF2UP回転考慮 縮小 A4x2 to A4 2UP (Crop対応)

ダウンロード -PDF2UP回転考慮縮小.zip



【スクリプトエディタで開く】

#!/usr/bin/env python3
import sys
import math
from pypdf import PdfReader
from pypdf import PdfWriter
from pypdf import PageObject
from pypdf import Transformation
from pypdf.generic import RectangleObject


#########################################入力ファイル受け取り
argGetData = sys.argv
strFilePath = str(argGetData[1])

###テスト用
##strFilePath = "/path/to/pdf.pdf"


strOutputPDFDocPath = strFilePath + ".2UP.pdf"
try:
  with open(strFilePath, 'rb') as objInputData:
#########################################ライター初期化
    objReader = PdfReader(objInputData)
    objWriter = PdfWriter()
#########################################ページ数調べる
    objAllPages = objReader.pages
    numAllPage = int(len(objAllPages))
    print(numAllPage)
    numRepeatTImes = math.ceil(numAllPage / 2 )
    print(numRepeatTImes)
#########################################ページ数2回数繰り返し
    numCntInPage = int(0)
    for numTimes in range(numRepeatTImes):
########################################右ページ取り出し
      objPageR = objReader.pages[numCntInPage]
      numCropXR = objPageR.cropbox.left
      numCropYR = objPageR.cropbox.bottom
      numCropWR = objPageR.cropbox.right
      numCropHR = objPageR.cropbox.top
      print("numCropXR:" , numCropXR)
      print("numCropYR:" , numCropYR)
      print("numCropWR:" , numCropWR)
      print("numCropHR:" , numCropHR)

      numRotationR = objPageR.rotation
      print("Rotation:" , numRotationR)
      if (numRotationR == 0) | (numRotationR == 180) :
        numWR = numCropWR - numCropXR
        numHR = numCropHR - numCropYR
      elif (numRotationR == 90) | (numRotationR == 270) :
        numWR = numCropHR - numCropYR
        numHR = numCropWR - numCropXR
      print("R :" , numWR , "x" , numHR)

      objPageR.mediabox = RectangleObject((numCropXR,numCropYR,numCropWR,numCropHR  ))
      objPageR.cropbox = RectangleObject((numCropXR,numCropYR,numCropWR,numCropHR  ))
      objPageR.trimbox = RectangleObject((numCropXR,numCropYR,numCropWR,numCropHR  ))
      objPageR.bleedbox = RectangleObject((numCropXR,numCropYR,numCropWR ,numCropHR  ))
      objPageR.artbox = RectangleObject((numCropXR,numCropYR,numCropWR ,numCropHR ))


#########################################奇数ページの場合
      if numCntInPage == (numAllPage - 1):
        objPageL = PageObject.create_blank_page(width=numWR, height=numHR)
        numWL = numWR
        numHL = numHR
      else:
        numCntInPage = numCntInPage + 1
        ########################################左ページ取り出し
        objPageL = objReader.pages[numCntInPage]
        numCropXL = objPageL.cropbox.left
        numCropYL = objPageL.cropbox.bottom
        numCropWL = objPageL.cropbox.right
        numCropHL = objPageL.cropbox.top
        print("numCropXL:" , numCropXL)
        print("numCropYL:" , numCropYL)
        print("numCropWL:" , numCropWL)
        print("numCropHL:" , numCropHL)
        objPageL.mediabox = RectangleObject((numCropXL,numCropYL,numCropWL,numCropHL))
        objPageL.cropbox = RectangleObject((numCropXL,numCropYL,numCropWL,numCropHL))
        objPageL.trimbox = RectangleObject((numCropXL,numCropYL,numCropWL,numCropHL))
        objPageL.bleedbox = RectangleObject((numCropXL,numCropYL,numCropWL,numCropHL))
        objPageL.artbox = RectangleObject((numCropXL,numCropYL,numCropWL,numCropHL))


        numRotationL = objPageL.rotation
        print("Rotation:" , numRotationL)
        if (numRotationL == 0) | (numRotationL == 180) :
          numWL = numCropWL - numCropXL
          numHL = numCropHL - numCropYL
        elif (numRotationL == 90) | (numRotationL == 270) :
          numWL = numCropHL - numCropYL
          numHL = numCropWL - numCropXL
        print("L :" , numWL , "x" , numHL)
        numCntInPage = numCntInPage + 1
      #sys.exit(0)  
#########################################新規ページの高さは最大値
      if numHR >= numHL:
        numNewH = numHR
      else:
        numNewH = numHL
#########################################新規ページの幅
      numNewW = numWL + numWL
#########################################左右サイズで新規ページ
      print("2W:" , numNewW , "x" , numNewH)
      objNewPage =  PageObject.create_blank_page(None,numNewW,numNewH)
#########################################右側ページのリサイズ
      if (numRotationR == 0):
        numNewRotationR = 0
        numTXR = 0 - numCropXR
        numTYR = 0 - numCropYR
      elif (numRotationR == 90):
        numNewRotationR = 270
        numTXR = 0 - numCropYR
        numTYR = numHR + numCropXR
      elif (numRotationR == 180):
        numNewRotationR = 180
        numTXR =  numWR + numCropXR
        numTYR = numHR + numCropYR
      elif (numRotationR == 270):
        numNewRotationR = 90
        numTXR = (numWR) + numCropYR
        numTYR = numCropHR - numCropHL - numCropXR
      objOption = Transformation().rotate(numNewRotationR).translate(tx=numTXR, ty=numTYR)
      objPageR.add_transformation(objOption,True)
#     objPageR.mediabox = RectangleObject((0,0,numNewW,numNewH))
#     objPageR.cropbox = RectangleObject((0,0,numNewW,numNewH))
#     objPageR.trimbox = RectangleObject((0,0,numNewW,numNewH))
#     objPageR.bleedbox = RectangleObject((0,0,numNewW,numNewH))
#     objPageR.artbox = RectangleObject((0,0,numNewW,numNewH))

      objPageR.mediabox = RectangleObject((0,0,numWR,numNewH))
      objPageR.cropbox = RectangleObject((0,0,numWR,numNewH))
      objPageR.trimbox = RectangleObject((0,0,numWR,numNewH))
      objPageR.bleedbox = RectangleObject((0,0,numWR,numNewH))
      objPageR.artbox = RectangleObject((0,0,numWR,numNewH))

      objNewPage.merge_page(objPageR,True)



#########################################左側ページを新規ページに
      if (numRotationL == 0):
        numNewRotationL = 0
        numTXL = numWL - numCropXL
        numTYL = 0 - numCropYL
      elif (numRotationL == 90):
        numNewRotationL = 270
        numTXL = numWL - numCropYL
        numTYL = numHL + numCropXL
      elif (numRotationL == 180):
        numNewRotationL = 180
        numTXL = numWL + numWR + numCropXL
        numTYL = numHL + numCropYL
      elif (numRotationL == 270):
        numNewRotationL = 90
        numTXL = numNewW + numCropYL
        numTYL = 0 - numCropXL
      objOption = Transformation().rotate(numNewRotationL).translate(tx=numTXL, ty=numTYL)
      objPageL.add_transformation(objOption,True)
#####ページサイズを確定
#     objPageL.mediabox = RectangleObject((0,0,numNewW,numNewH))
#     objPageL.cropbox = RectangleObject((0,0,numNewW,numNewH))
#     objPageL.trimbox = RectangleObject((0,0,numNewW,numNewH))
#     objPageL.bleedbox = RectangleObject((0,0,numNewW,numNewH))
#     objPageL.artbox = RectangleObject((0,0,numNewW,numNewH))

      objPageL.mediabox = RectangleObject((numWL,0,numNewW,numNewH))
      objPageL.cropbox = RectangleObject((numWL,0,numNewW,numNewH))
      objPageL.trimbox = RectangleObject((numWL,0,numNewW,numNewH))
      objPageL.bleedbox = RectangleObject((numWL,0,numNewW,numNewH))
      objPageL.artbox = RectangleObject((numWL,0,numNewW,numNewH))


      objNewPage.merge_page(objPageL,False)

#########################################新規ページのサイズ
      objNewPage.mediabox = RectangleObject((0,0,numNewW,numNewH))
      objNewPage.cropbox = RectangleObject((0,0,numNewW,numNewH))
      objNewPage.trimbox = RectangleObject((0,0,numNewW,numNewH))
      objNewPage.bleedbox = RectangleObject((0,0,numNewW,numNewH))
      objNewPage.artbox = RectangleObject((0,0,numNewW,numNewH))
########################################objNewPageを縮小する
      if numNewW >= (numNewH / 2):
        ratioScaleW = numNewW / 2
        ratioScaleH = numNewH / 2
        objNewPage.scale_to(ratioScaleW,ratioScaleH)
      else:
        ratioScaleW = numNewW
        ratioScaleH = numNewH / 2
        objNewPage.scale_to(ratioScaleW,ratioScaleH)
#########################################縮小後のページサイズ
      objNewPage.mediabox = RectangleObject((0,0,ratioScaleW,ratioScaleH))
      objNewPage.cropbox = RectangleObject((0,0,ratioScaleW,ratioScaleH))
      objNewPage.trimbox = RectangleObject((0,0,ratioScaleW,ratioScaleH))
      objNewPage.bleedbox = RectangleObject((0,0,ratioScaleW,ratioScaleH))
      objNewPage.artbox = RectangleObject((0,0,ratioScaleW,ratioScaleH))



      objWriter.add_page(objNewPage)
except FileNotFoundError:
  print("ファイルのオープンに失敗しました")
  sys.exit(0
try:
#########################################
  with open(strOutputPDFDocPath, 'wb') as objOutPutData:
    objWriter.write(objOutPutData)
except FileNotFoundError:
    print("ファイルの保存に失敗しました")

sys.exit(0


|

[python3]PDF2UP回転考慮 上下開き 修正

ダウンロード - PDF2UP回転考慮上下.zip


【スクリプトエディタで開く】

#!/usr/bin/env python3
import sys
import math
from pypdf import PdfReader
from pypdf import PdfWriter
from pypdf import PageObject
from pypdf import Transformation
from pypdf.generic import RectangleObject

#########################################入力ファイル受け取り
argGetData = sys.argv
strFilePath = str(argGetData[1])

###テスト用
##strFilePath = "/path/to/pdf.pdf"

strOutputPDFDocPath = strFilePath + ".2in1.pdf"
try:
  with open(strFilePath, 'rb') as objInputData:
#########################################ライター初期化
    objReader = PdfReader(objInputData)
    objWriter = PdfWriter()
#########################################ページ数調べる
    objAllPages = objReader.pages
    numAllPage = int(len(objAllPages))
    print(numAllPage)
    numRepeatTImes = math.ceil(numAllPage / 2 )
    print(numRepeatTImes)
#########################################ページ数2回数繰り返し
    numCntInPage = int(0)
    for numTimes in range(numRepeatTImes):
########################################右ページ取り出し
      objPageR = objReader.pages[numCntInPage]
      numCropXR = objPageR.cropbox.left
      numCropYR = objPageR.cropbox.bottom
      numCropWR = objPageR.cropbox.right
      numCropHR = objPageR.cropbox.top
      print("numCropXR:" , numCropXR)
      print("numCropYR:" , numCropYR)
      print("numCropWR:" , numCropWR)
      print("numCropHR:" , numCropHR)
      rectCropBoxR = objPageR.cropbox
      objPageR.mediabox = RectangleObject((numCropXR,numCropYR,numCropWR,numCropHR))
      objPageR.cropbox = RectangleObject((numCropXR,numCropYR,numCropWR,numCropHR))
      objPageR.trimbox = RectangleObject((numCropXR,numCropYR,numCropWR,numCropHR))
      objPageR.bleedbox = RectangleObject((numCropXR,numCropYR,numCropWR,numCropHR))
      objPageR.artbox = rectCropBoxR

      numRotationR = objPageR.rotation
      print("Rotation:" , numRotationR)
      if (numRotationR == 0) | (numRotationR == 180) :
        numWR = numCropWR - numCropXR
        numHR = numCropHR - numCropYR
      elif (numRotationR == 90) | (numRotationR == 270) :
        numWR = numCropHR - numCropYR
        numHR = numCropWR - numCropXR
      print("R :" , numWR , "x" , numHR)
#########################################奇数ページの場合
      if numCntInPage == (numAllPage - 1):
        objPageL = PageObject.create_blank_page(width=numWR, height=numHR)
        numWL = numWR
        numHL = numHR
      else:
        numCntInPage = numCntInPage + 1
########################################左ページ取り出し
        objPageL = objReader.pages[numCntInPage]
        numCropXL = objPageL.cropbox.left
        numCropYL = objPageL.cropbox.bottom
        numCropWL = objPageL.cropbox.right
        numCropHL = objPageL.cropbox.top
        print("numCropXL:" , numCropXL)
        print("numCropYL:" , numCropYL)
        print("numCropWL:" , numCropWL)
        print("numCropHL:" , numCropHL)
        rectCropBoxL = objPageL.cropbox
        objPageL.mediabox = RectangleObject((numCropXL,numCropYL,numCropWL,numCropHL))
        objPageL.cropbox = RectangleObject((numCropXL,numCropYL,numCropWL,numCropHL))
        objPageL.trimbox = RectangleObject((numCropXL,numCropYL,numCropWL,numCropHL))
        objPageL.bleedbox = RectangleObject((numCropXL,numCropYL,numCropWL,numCropHL))
        objPageL.artbox = rectCropBoxL

        numRotationL = objPageL.rotation
        print("Rotation:" , numRotationL)
        if (numRotationL == 0) | (numRotationL == 180) :
          numWL = numCropWL - numCropXL
          numHL = numCropHL - numCropYL
        elif (numRotationL == 90) | (numRotationL == 270) :
          numWL = numCropHL - numCropYL
          numHL = numCropWL - numCropXL
        print("L :" , numWL , "x" , numHL)
        numCntInPage = numCntInPage + 1
      #sys.exit(0)  
#########################################新規ページの高
        numNewH = numHR + numHL
#########################################新規ページの幅は最大値
      if numWR >= numWL:
        numNewW = numWR
      else:
        numNewW = numWL
#########################################上下サイズで新規ページ
      print("2W:" , numNewW , "x" , numNewH)
      objNewPage =  PageObject.create_blank_page(None,numNewW,numNewH)
#########################################下側ページを新規ページに
      if (numRotationL == 0):
        numNewRotationL = 0
        numTXL = 0 - numCropXL
        numTYL = 0 - numCropYL
      elif (numRotationL == 90):
        numNewRotationL = 270
        numTXL = 0 - numCropYR
        numTYL = numHL + numCropXL
      elif (numRotationL == 180):
        numNewRotationL = 180
        numTXL = numWL + numCropXL
        numTYL = numHL + numCropYL
      elif (numRotationL == 270):
        numNewRotationL = 90
        numTXL = numWL +  numCropYL
        numTYL = 0 - numCropXL
      objOption = Transformation().rotate(numNewRotationL).translate(tx=numTXL, ty=numTYL)
      objPageL.add_transformation(objOption,True)

      objPageL.mediabox = RectangleObject((0,0,numWL,numHL))
      objPageL.cropbox = RectangleObject((0,0,numWL,numHL))
      objPageL.trimbox = RectangleObject((0,0,numWL,numHL))
      objPageL.bleedbox = RectangleObject((0,0,numWL,numHL))
      objPageL.artbox = RectangleObject((0,0,numWL,numHL))

      objNewPage.merge_page(objPageL,False)
#########################################上側ページのリサイズ
      if (numRotationR == 0):
        numNewRotationR = 0
        numTXR = 0 - numCropXR
        numTYR = numHR - numCropYR
      elif (numRotationR == 90):
        numNewRotationR = 270
        numTXR = 0 - numCropYR
        numTYR = numHR + numHL  + numCropXR
      elif (numRotationR == 180):
        numNewRotationR = 180
        numTXR = numWR + numCropXR
        numTYR = numHR + numHL + numCropYR
      elif (numRotationR == 270):
        numNewRotationR = 90
        numTXR = numWR + numCropYR
        numTYR = numHL - numCropXR
      objOption = Transformation().rotate(numNewRotationR).translate(tx=numTXR, ty=numTYR)
      objPageR.add_transformation(objOption,True)
      objPageR.mediabox = RectangleObject((0,numHL,numWR,numNewH))
      objPageR.cropbox = RectangleObject((0,numHL,numWR,numNewH))
      objPageR.trimbox = RectangleObject((0,numHL,numWR,numNewH))
      objPageR.bleedbox = RectangleObject((0,numHL,numWR,numNewH))
      objPageR.artbox = RectangleObject((0,numHL,numWR,numNewH))
      objNewPage.merge_page(objPageR,True)
#########################################新規ページのサイズ
      objNewPage.mediabox = RectangleObject((0,0,numNewW,numNewH))
      objNewPage.cropbox = RectangleObject((0,0,numNewW,numNewH))
      objNewPage.trimbox = RectangleObject((0,0,numNewW,numNewH))
      objNewPage.bleedbox = RectangleObject((0,0,numNewW,numNewH))
      objNewPage.artbox = RectangleObject((0,0,numNewW,numNewH))
      objWriter.add_page(objNewPage)
except FileNotFoundError:
  print("ファイルのオープンに失敗しました")
  sys.exit(0
try:
#########################################
  with open(strOutputPDFDocPath, 'wb') as objOutPutData:
    objWriter.write(objOutPutData)
except FileNotFoundError:
    print("ファイルの保存に失敗しました")

sys.exit(0


|

[python3]PDF2UP回転考慮 縮小 A4x2 to A4 2UP (Bleed対応)

ダウンロード - PDF2UP回転考慮縮小.zip



【スクリプトエディタで開く】

#!/usr/bin/env python3
import sys
import math
from pypdf import PdfReader
from pypdf import PdfWriter
from pypdf import PageObject
from pypdf import Transformation
from pypdf.generic import RectangleObject


#########################################入力ファイル受け取り
argGetData = sys.argv
strFilePath = str(argGetData[1])

###テスト用
##strFilePath = "/path/to/pdf.pdf"


strOutputPDFDocPath = strFilePath + ".2UP.pdf"
try:
  with open(strFilePath, 'rb') as objInputData:
#########################################ライター初期化
    objReader = PdfReader(objInputData)
    objWriter = PdfWriter()
#########################################ページ数調べる
    objAllPages = objReader.pages
    numAllPage = int(len(objAllPages))
    print(numAllPage)
    numRepeatTImes = math.ceil(numAllPage / 2 )
    print(numRepeatTImes)
#########################################ページ数2回数繰り返し
    numCntInPage = int(0)
    for numTimes in range(numRepeatTImes):
########################################右ページ取り出し
      objPageR = objReader.pages[numCntInPage]
      numCropXR = objPageR.cropbox.left
      numCropYR = objPageR.cropbox.bottom
      numCropWR = objPageR.cropbox.right
      numCropHR = objPageR.cropbox.top
      print("numCropXR:" , numCropXR)
      print("numCropYR:" , numCropYR)
      print("numCropWR:" , numCropWR)
      print("numCropHR:" , numCropHR)

      numRotationR = objPageR.rotation
      print("Rotation:" , numRotationR)
      if (numRotationR == 0) | (numRotationR == 180) :
        numWR = numCropWR - numCropXR
        numHR = numCropHR - numCropYR
      elif (numRotationR == 90) | (numRotationR == 270) :
        numWR = numCropHR - numCropYR
        numHR = numCropWR - numCropXR
      print("R :" , numWR , "x" , numHR)

      objPageR.mediabox = RectangleObject((numCropXR,numCropYR,numCropWR,numCropHR  ))
      objPageR.cropbox = RectangleObject((numCropXR,numCropYR,numCropWR,numCropHR  ))
      objPageR.trimbox = RectangleObject((numCropXR,numCropYR,numCropWR,numCropHR  ))
      objPageR.bleedbox = RectangleObject((numCropXR,numCropYR,numCropWR ,numCropHR  ))
      objPageR.artbox = RectangleObject((numCropXR,numCropYR,numCropWR ,numCropHR ))


#########################################奇数ページの場合
      if numCntInPage == (numAllPage - 1):
        objPageL = PageObject.create_blank_page(width=numWR, height=numHR)
        numWL = numWR
        numHL = numHR
      else:
        numCntInPage = numCntInPage + 1
        ########################################左ページ取り出し
        objPageL = objReader.pages[numCntInPage]
        numCropXL = objPageL.cropbox.left
        numCropYL = objPageL.cropbox.bottom
        numCropWL = objPageL.cropbox.right
        numCropHL = objPageL.cropbox.top
        print("numCropXL:" , numCropXL)
        print("numCropYL:" , numCropYL)
        print("numCropWL:" , numCropWL)
        print("numCropHL:" , numCropHL)
        objPageL.mediabox = RectangleObject((numCropXL,numCropYL,numCropWL,numCropHL))
        objPageL.cropbox = RectangleObject((numCropXL,numCropYL,numCropWL,numCropHL))
        objPageL.trimbox = RectangleObject((numCropXL,numCropYL,numCropWL,numCropHL))
        objPageL.bleedbox = RectangleObject((numCropXL,numCropYL,numCropWL,numCropHL))
        objPageL.artbox = RectangleObject((numCropXL,numCropYL,numCropWL,numCropHL))


        numRotationL = objPageL.rotation
        print("Rotation:" , numRotationL)
        if (numRotationL == 0) | (numRotationL == 180) :
          numWL = numCropWL - numCropXL
          numHL = numCropHL - numCropYL
        elif (numRotationL == 90) | (numRotationL == 270) :
          numWL = numCropHL - numCropYL
          numHL = numCropWL - numCropXL
        print("L :" , numWL , "x" , numHL)
        numCntInPage = numCntInPage + 1
      #sys.exit(0)  
#########################################新規ページの高さは最大値
      if numHR >= numHL:
        numNewH = numHR
      else:
        numNewH = numHL
#########################################新規ページの幅
      numNewW = numWL + numWL
#########################################左右サイズで新規ページ
      print("2W:" , numNewW , "x" , numNewH)
      objNewPage =  PageObject.create_blank_page(None,numNewW,numNewH)
#########################################右側ページのリサイズ
      if (numRotationR == 0):
        numNewRotationR = 0
        numTXR = 0 - numCropXR
        numTYR = 0 - numCropYR
      elif (numRotationR == 90):
        numNewRotationR = 270
        numTXR = 0 - numCropYR
        numTYR = numHR + numCropXR
      elif (numRotationR == 180):
        numNewRotationR = 180
        numTXR =  numWR + numCropXR
        numTYR = numHR + numCropYR
      elif (numRotationR == 270):
        numNewRotationR = 90
        numTXR = (numWR) + numCropYR
        numTYR = numCropHR - numCropHL - numCropXR
      objOption = Transformation().rotate(numNewRotationR).translate(tx=numTXR, ty=numTYR)
      objPageR.add_transformation(objOption,True)
#     objPageR.mediabox = RectangleObject((0,0,numNewW,numNewH))
#     objPageR.cropbox = RectangleObject((0,0,numNewW,numNewH))
#     objPageR.trimbox = RectangleObject((0,0,numNewW,numNewH))
#     objPageR.bleedbox = RectangleObject((0,0,numNewW,numNewH))
#     objPageR.artbox = RectangleObject((0,0,numNewW,numNewH))

      objPageR.mediabox = RectangleObject((0,0,numWR,numNewH))
      objPageR.cropbox = RectangleObject((0,0,numWR,numNewH))
      objPageR.trimbox = RectangleObject((0,0,numWR,numNewH))
      objPageR.bleedbox = RectangleObject((0,0,numWR,numNewH))
      objPageR.artbox = RectangleObject((0,0,numWR,numNewH))

      objNewPage.merge_page(objPageR,True)



#########################################左側ページを新規ページに
      if (numRotationL == 0):
        numNewRotationL = 0
        numTXL = numWL - numCropXL
        numTYL = 0 - numCropYL
      elif (numRotationL == 90):
        numNewRotationL = 270
        numTXL = numWL - numCropYL
        numTYL = numHL + numCropXL
      elif (numRotationL == 180):
        numNewRotationL = 180
        numTXL = numWL + numWR + numCropXL
        numTYL = numHL + numCropYL
      elif (numRotationL == 270):
        numNewRotationL = 90
        numTXL = numNewW + numCropYL
        numTYL = 0 - numCropXL
      objOption = Transformation().rotate(numNewRotationL).translate(tx=numTXL, ty=numTYL)
      objPageL.add_transformation(objOption,True)
#####ページサイズを確定
#     objPageL.mediabox = RectangleObject((0,0,numNewW,numNewH))
#     objPageL.cropbox = RectangleObject((0,0,numNewW,numNewH))
#     objPageL.trimbox = RectangleObject((0,0,numNewW,numNewH))
#     objPageL.bleedbox = RectangleObject((0,0,numNewW,numNewH))
#     objPageL.artbox = RectangleObject((0,0,numNewW,numNewH))

      objPageL.mediabox = RectangleObject((numWL,0,numNewW,numNewH))
      objPageL.cropbox = RectangleObject((numWL,0,numNewW,numNewH))
      objPageL.trimbox = RectangleObject((numWL,0,numNewW,numNewH))
      objPageL.bleedbox = RectangleObject((numWL,0,numNewW,numNewH))
      objPageL.artbox = RectangleObject((numWL,0,numNewW,numNewH))


      objNewPage.merge_page(objPageL,False)

#########################################新規ページのサイズ
      objNewPage.mediabox = RectangleObject((0,0,numNewW,numNewH))
      objNewPage.cropbox = RectangleObject((0,0,numNewW,numNewH))
      objNewPage.trimbox = RectangleObject((0,0,numNewW,numNewH))
      objNewPage.bleedbox = RectangleObject((0,0,numNewW,numNewH))
      objNewPage.artbox = RectangleObject((0,0,numNewW,numNewH))
########################################objNewPageを縮小する
      if numNewW >= numNewH:
        ratioScaleW = numNewH
        ratioScaleH = numNewW / 2
        objNewPage.scale_to(ratioScaleW,ratioScaleH)
      else:
        ratioScaleW = numNewW
        ratioScaleH = numNewH / 2
        objNewPage.scale_to(ratioScaleW,ratioScaleH)
#########################################縮小後のページサイズ
      objNewPage.mediabox = RectangleObject((0,0,ratioScaleW,ratioScaleH))
      objNewPage.cropbox = RectangleObject((0,0,ratioScaleW,ratioScaleH))
      objNewPage.trimbox = RectangleObject((0,0,ratioScaleW,ratioScaleH))
      objNewPage.bleedbox = RectangleObject((0,0,ratioScaleW,ratioScaleH))
      objNewPage.artbox = RectangleObject((0,0,ratioScaleW,ratioScaleH))



      objWriter.add_page(objNewPage)
except FileNotFoundError:
  print("ファイルのオープンに失敗しました")
  sys.exit(0
try:
#########################################
  with open(strOutputPDFDocPath, 'wb') as objOutPutData:
    objWriter.write(objOutPutData)
except FileNotFoundError:
    print("ファイルの保存に失敗しました")

sys.exit(0


|

[python3]PDF2UP回転考慮 少し修正(塗りたしBleedBOXに対応)

ダウンロード - pdf2upe59b9ee8bba2e88083e685ae.zip



【スクリプトエディタで開く】

#!/usr/bin/env python3
import sys
import math
from pypdf import PdfReader
from pypdf import PdfWriter
from pypdf import PageObject
from pypdf import Transformation
from pypdf.generic import RectangleObject


#########################################入力ファイル受け取り
argGetData = sys.argv
strFilePath = str(argGetData[1])

###テスト用
##strFilePath = "/path/to/pdf.pdf"

strOutputPDFDocPath = strFilePath + ".2UP.pdf"
try:
  with open(strFilePath, 'rb') as objInputData:
#########################################ライター初期化
    objReader = PdfReader(objInputData)
    objWriter = PdfWriter()
#########################################ページ数調べる
    objAllPages = objReader.pages
    numAllPage = int(len(objAllPages))
    print(numAllPage)
    numRepeatTImes = math.ceil(numAllPage / 2 )
    print(numRepeatTImes)
#########################################ページ数2回数繰り返し
    numCntInPage = int(0)
    for numTimes in range(numRepeatTImes):
########################################右ページ取り出し
      objPageR = objReader.pages[numCntInPage]
      numCropXR = objPageR.cropbox.left
      numCropYR = objPageR.cropbox.bottom
      numCropWR = objPageR.cropbox.right
      numCropHR = objPageR.cropbox.top
      print("numCropXR:" , numCropXR)
      print("numCropYR:" , numCropYR)
      print("numCropWR:" , numCropWR)
      print("numCropHR:" , numCropHR)

      numRotationR = objPageR.rotation
      print("Rotation:" , numRotationR)
      if (numRotationR == 0) | (numRotationR == 180) :
        numWR = numCropWR - numCropXR
        numHR = numCropHR - numCropYR
      elif (numRotationR == 90) | (numRotationR == 270) :
        numWR = numCropHR - numCropYR
        numHR = numCropWR - numCropXR
      print("R :" , numWR , "x" , numHR)

      objPageR.mediabox = RectangleObject((numCropXR,numCropYR,numCropWR,numCropHR  ))
      objPageR.cropbox = RectangleObject((numCropXR,numCropYR,numCropWR,numCropHR  ))
      objPageR.trimbox = RectangleObject((numCropXR,numCropYR,numCropWR,numCropHR  ))
      objPageR.bleedbox = RectangleObject((numCropXR,numCropYR,numCropWR ,numCropHR  ))
      objPageR.artbox = RectangleObject((numCropXR,numCropYR,numCropWR ,numCropHR ))


#########################################奇数ページの場合
      if numCntInPage == (numAllPage - 1):
        objPageL = PageObject.create_blank_page(width=numWR, height=numHR)
        numWL = numWR
        numHL = numHR
      else:
        numCntInPage = numCntInPage + 1
        ########################################左ページ取り出し
        objPageL = objReader.pages[numCntInPage]
        numCropXL = objPageL.cropbox.left
        numCropYL = objPageL.cropbox.bottom
        numCropWL = objPageL.cropbox.right
        numCropHL = objPageL.cropbox.top
        print("numCropXL:" , numCropXL)
        print("numCropYL:" , numCropYL)
        print("numCropWL:" , numCropWL)
        print("numCropHL:" , numCropHL)
        objPageL.mediabox = RectangleObject((numCropXL,numCropYL,numCropWL,numCropHL))
        objPageL.cropbox = RectangleObject((numCropXL,numCropYL,numCropWL,numCropHL))
        objPageL.trimbox = RectangleObject((numCropXL,numCropYL,numCropWL,numCropHL))
        objPageL.bleedbox = RectangleObject((numCropXL,numCropYL,numCropWL,numCropHL))
        objPageL.artbox = RectangleObject((numCropXL,numCropYL,numCropWL,numCropHL))


        numRotationL = objPageL.rotation
        print("Rotation:" , numRotationL)
        if (numRotationL == 0) | (numRotationL == 180) :
          numWL = numCropWL - numCropXL
          numHL = numCropHL - numCropYL
        elif (numRotationL == 90) | (numRotationL == 270) :
          numWL = numCropHL - numCropYL
          numHL = numCropWL - numCropXL
        print("L :" , numWL , "x" , numHL)
        numCntInPage = numCntInPage + 1
      #sys.exit(0)  
#########################################新規ページの高さは最大値
      if numHR >= numHL:
        numNewH = numHR
      else:
        numNewH = numHL
#########################################新規ページの幅
      numNewW = numWL + numWL
#########################################左右サイズで新規ページ
      print("2W:" , numNewW , "x" , numNewH)
      objNewPage =  PageObject.create_blank_page(None,numNewW,numNewH)
#########################################右側ページのリサイズ
      if (numRotationR == 0):
        numNewRotationR = 0
        numTXR = 0 - numCropXR
        numTYR = 0 - numCropYR
      elif (numRotationR == 90):
        numNewRotationR = 270
        numTXR = 0 - numCropYR
        numTYR = numHR + numCropXR
      elif (numRotationR == 180):
        numNewRotationR = 180
        numTXR =  numWR + numCropXR
        numTYR = numHR + numCropYR
      elif (numRotationR == 270):
        numNewRotationR = 90
        numTXR = (numWR) + numCropYR
        numTYR = numCropHR - numCropHL - numCropXR
      objOption = Transformation().rotate(numNewRotationR).translate(tx=numTXR, ty=numTYR)
      objPageR.add_transformation(objOption,True)
#     objPageR.mediabox = RectangleObject((0,0,numNewW,numNewH))
#     objPageR.cropbox = RectangleObject((0,0,numNewW,numNewH))
#     objPageR.trimbox = RectangleObject((0,0,numNewW,numNewH))
#     objPageR.bleedbox = RectangleObject((0,0,numNewW,numNewH))
#     objPageR.artbox = RectangleObject((0,0,numNewW,numNewH))

      objPageR.mediabox = RectangleObject((0,0,numWR,numNewH))
      objPageR.cropbox = RectangleObject((0,0,numWR,numNewH))
      objPageR.trimbox = RectangleObject((0,0,numWR,numNewH))
      objPageR.bleedbox = RectangleObject((0,0,numWR,numNewH))
      objPageR.artbox = RectangleObject((0,0,numWR,numNewH))

      objNewPage.merge_page(objPageR,True)



#########################################左側ページを新規ページに
      if (numRotationL == 0):
        numNewRotationL = 0
        numTXL = numWL - numCropXL
        numTYL = 0 - numCropYL
      elif (numRotationL == 90):
        numNewRotationL = 270
        numTXL = numWL - numCropYL
        numTYL = numHL + numCropXL
      elif (numRotationL == 180):
        numNewRotationL = 180
        numTXL = numWL + numWR + numCropXL
        numTYL = numHL + numCropYL
      elif (numRotationL == 270):
        numNewRotationL = 90
        numTXL = numNewW + numCropYL
        numTYL = 0 - numCropXL
      objOption = Transformation().rotate(numNewRotationL).translate(tx=numTXL, ty=numTYL)
      objPageL.add_transformation(objOption,True)
#####ページサイズを確定
#     objPageL.mediabox = RectangleObject((0,0,numNewW,numNewH))
#     objPageL.cropbox = RectangleObject((0,0,numNewW,numNewH))
#     objPageL.trimbox = RectangleObject((0,0,numNewW,numNewH))
#     objPageL.bleedbox = RectangleObject((0,0,numNewW,numNewH))
#     objPageL.artbox = RectangleObject((0,0,numNewW,numNewH))

      objPageL.mediabox = RectangleObject((numWL,0,numNewW,numNewH))
      objPageL.cropbox = RectangleObject((numWL,0,numNewW,numNewH))
      objPageL.trimbox = RectangleObject((numWL,0,numNewW,numNewH))
      objPageL.bleedbox = RectangleObject((numWL,0,numNewW,numNewH))
      objPageL.artbox = RectangleObject((numWL,0,numNewW,numNewH))


      objNewPage.merge_page(objPageL,False)

#########################################新規ページのサイズ
#     objNewPage.mediabox = RectangleObject((0,0,numNewW,numNewH))
#     objNewPage.cropbox = RectangleObject((0,0,numNewW,numNewH))
#     objNewPage.trimbox = RectangleObject((0,0,numNewW,numNewH))
#     objNewPage.bleedbox = RectangleObject((0,0,numNewW,numNewH))
#     objNewPage.artbox = RectangleObject((0,0,numNewW,numNewH))
      objWriter.add_page(objNewPage)
except FileNotFoundError:
  print("ファイルのオープンに失敗しました")
  sys.exit(0
try:
#########################################
  with open(strOutputPDFDocPath, 'wb') as objOutPutData:
    objWriter.write(objOutPutData)
except FileNotFoundError:
    print(