開発環境
- OS X El Capitan - Apple (OS)
- Emacs (Text Editor)
- Python 3.5 (プログラミング言語)
Automate the Boring Stuff with Python (Al Sweigart (著)、No Starch Press)のPart 2.(Automating Tasks)、Chapter 13.(Working with PDF and word Documents)、Practice Projects(PDF Paranoia)(No. 7803)を取り組んでみる。
Practice Projects(PDF Paranoia)(No. 7803)
コード(Emacs)
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import sys
import PyPDF2
if len(sys.argv) >= 2:
PASSWORD = sys.argv[1]
else:
print('usage: cmd password')
sys.exit(1)
path = '../'
FILENAME = None
for folder_name, _, filenames in os.walk(path):
for filename in filenames:
if filename.endswith('.pdf'):
filename = folder_name + os.sep + filename
if FILENAME is None:
FILENAME = filename + '_encrypted.pdf'
with open(filename, 'rb') as pdf_file:
try:
pdf_reader = PyPDF2.PdfFileReader(pdf_file)
if not pdf_reader.isEncrypted:
print(filename)
pdf_writer = PyPDF2.PdfFileWriter()
pdf_writer.encrypt(PASSWORD)
for page_num in range(pdf_reader.numPages):
page = pdf_reader.getPage(page_num)
pdf_writer.addPage(page)
with open(filename + '_encrypted.pdf', 'wb') as f:
pdf_writer.write(f)
except Exception as err:
print('{0}: {1}'.format(filename, err))
with open(FILENAME, 'rb') as f:
pdf_reader = PyPDF2.PdfFileReader(f)
print(pdf_reader.isEncrypted)
pdf_reader.decrypt(PASSWORD)
page = pdf_reader.getPage(0)
print(page.extractText())
入出力結果(Terminal, IPython)
$ find ../ -name '*.pdf' ..//automate_online-materials/combinedminutes.pdf ..//automate_online-materials/encrypted.pdf ..//automate_online-materials/encryptedminutes.pdf ..//automate_online-materials/meetingminutes.pdf ..//automate_online-materials/meetingminutes2.pdf ..//automate_online-materials/watermark.pdf ..//Automate_the_Boring_Stuff_sample_ch17.pdf ..//ch13/combinedminutes.pdf ..//ch13/encrypted.pdf ..//ch13/meetingminutes.pdf ..//ch13/meetingminutes2.pdf $ ./pdf_paranoia.py usage: cmd password $ ./pdf_paranoia.py password ..//Automate_the_Boring_Stuff_sample_ch17.pdf ../automate_online-materials/combinedminutes.pdf ../automate_online-materials/meetingminutes.pdf ../automate_online-materials/meetingminutes2.pdf ../automate_online-materials/watermark.pdf ../ch13/combinedminutes.pdf ../ch13/meetingminutes.pdf ../ch13/meetingminutes2.pdf True 17MA NI PUL ATING IMAGE SIf you have a digital camera or even if you just upload photos from your phone to Facebook, you probably cross paths with digital image ˜les all the time. You may know how to use basic graphics software, such as Microsoft Paint or Paintbrush, or even more advanced applica -tions such as Adobe Photoshop. But if you need to edit a massive number of images, editing them by hand can be a lengthy, boring job. Enter Python. Pillow is a third-party Python module for interacting with image ˜les. The module has several functions that make it easy to crop, resize, and edit the content of an image. With the power to manipu -late images the same way you would with software such as Microsoft Paint or Adobe Photoshop, Python can automatically edit hundreds or thousands of images with ease. $ find ../ -name '*.pdf' ..//automate_online-materials/combinedminutes.pdf ..//automate_online-materials/combinedminutes.pdf_encrypted.pdf ..//automate_online-materials/encrypted.pdf ..//automate_online-materials/encryptedminutes.pdf ..//automate_online-materials/meetingminutes.pdf ..//automate_online-materials/meetingminutes.pdf_encrypted.pdf ..//automate_online-materials/meetingminutes2.pdf ..//automate_online-materials/meetingminutes2.pdf_encrypted.pdf ..//automate_online-materials/watermark.pdf ..//automate_online-materials/watermark.pdf_encrypted.pdf ..//Automate_the_Boring_Stuff_sample_ch17.pdf ..//Automate_the_Boring_Stuff_sample_ch17.pdf_encrypted.pdf ..//ch13/combinedminutes.pdf ..//ch13/combinedminutes.pdf_encrypted.pdf ..//ch13/encrypted.pdf ..//ch13/meetingminutes.pdf ..//ch13/meetingminutes.pdf_encrypted.pdf ..//ch13/meetingminutes2.pdf ..//ch13/meetingminutes2.pdf_encrypted.pdf $
0 コメント:
コメントを投稿