Last active
August 12, 2021 08:40
-
-
Save nkymut/d881a7a4b2619b2068dc051e820eca11 to your computer and use it in GitHub Desktop.
自炊した四間飛車を指しこなす本 を読めるように直すスクリプト
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
#四間飛車を指しこなす本を直すスクリプト | |
#!pip install pypdf2 | |
#参考:https://realpython.com/pdf-python/ | |
from PyPDF2 import PdfFileReader, PdfFileWriter | |
#本文最初のPDFページ数 | |
start_page = 8 | |
#本文最後のPDFページ数 | |
end_page = 273 | |
#PDF総ページ数 | |
book_end = 276 | |
#目次まで | |
intro_range = range(0, start_page - 1) | |
#本文奇数ページ範囲 | |
odd_range = range(start_page, end_page - 1, 2) | |
#本文偶数ページ範囲を逆順に | |
even_range = range(end_page - 2, start_page - 1, -2) | |
#巻末のページ範囲 | |
end_range = range(end_page, book_end) | |
if __name__ == '__main__': | |
path = './shikenbisha/0002.pdf' #元ファイル | |
path_fixed = './shikenbisha/0002_fixed.pdf' #修正後ファイル | |
pages = [] | |
pdf_fixed = PdfFileWriter() | |
with open(path, 'rb') as f: | |
pdf = PdfFileReader(f) | |
information = pdf.getDocumentInfo() | |
number_of_pages = pdf.getNumPages() | |
if number_of_pages > 0: | |
#最初のページ追加 | |
for i in intro_range: | |
pdf_fixed.addPage(pdf.getPage(i)) | |
#奇数ページ追加 | |
for j in odd_range: | |
pdf_fixed.addPage(pdf.getPage(j)) | |
#偶数ページを上下反転して追加 | |
for k in even_range: | |
p = pdf.getPage(k) | |
p.rotateClockwise(180) | |
pdf_fixed.addPage(p) | |
#最後のページを追加 | |
for l in end_range: | |
pdf_fixed.addPage(pdf.getPage(l)) | |
#ファイルをセーブ | |
with open(path_fixed,"wb") as fh: | |
pdf_fixed.write(fh) | |
fh.close() | |
f.close() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment