Last active
April 14, 2019 09:29
-
-
Save gologius/3c6a207f2a0b56242f636fb7ab3bb472 to your computer and use it in GitHub Desktop.
PDFをZIPに変換します。PDFファイル名を引数として複数指定でき、1PDFにつき1ZIPファイル生成します。
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
# -*- coding: utf-8 -*- | |
""" | |
Created on Sun Apr 14 12:26:28 2019 | |
@author: gologius | |
""" | |
import datetime | |
import os | |
import pdf2image | |
import sys | |
import shutil | |
import tempfile | |
def check_file(filepath): | |
print("check file: "+ filepath) | |
root, ext = os.path.splitext(filepath) | |
if ext != '.pdf' or filepath=='': | |
return False | |
return True | |
def pdf2zip(filepath): | |
print('begin pdf to img') | |
with tempfile.TemporaryDirectory() as tmppath: | |
#なぜかoutput_foloderを指定して、一回ppmファイルとして出力しないと、PNGファイルとして出力できない | |
pages = pdf2image.convert_from_path(filepath, dpi=200, output_folder=tmppath) | |
basepath = os.path.dirname(filepath) | |
dirpath = basepath + "\\" + datetime.datetime.now().strftime("%Y%m%d_%H%M%S") | |
os.mkdir(dirpath) | |
print("make directory: " + dirpath) | |
for idx, page in enumerate(pages): | |
filename = '{0:04d}'.format(idx) + ".jpg" | |
page.save(dirpath + "\\" +filename, 'JPEG') | |
print(filename + ' ' + str(idx) + "/" + str(len(pages))) | |
print('end pdf to img') | |
#圧縮 | |
print('begin img to zip') | |
shutil.make_archive(dirpath, 'zip', root_dir=dirpath) | |
shutil.rmtree(dirpath) #不要フォルダを削除 | |
print('end img to zip') | |
def main(): | |
if len(sys.argv) == 1: | |
print('引数にファイルパスが指定されていません') | |
return | |
for i,path in enumerate(sys.argv): | |
if i==0: | |
continue #0番目は引数ではないのでスキップする | |
result = check_file(path) | |
if result == False: | |
print('不正なファイルです') | |
continue | |
pdf2zip(path) | |
return | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment