#!/usr/bin/env python
# coding=utf-8
from PyPDF2 import PdfFileReader, PdfFileWriter
# PDF文件分割
def split_pdf(read_file, out_detail):
try:
fp_read_file = open(read_file, 'rb')
pdf_input = PdfFileReader(fp_read_file) # 將要分割的PDF內容格式話
page_count = pdf_input.getNumPages() # 獲取PDF頁數
print(page_count) # 打印頁數
with open(out_detail, 'r',True,'utf-8')as fp:
# print(fp)
txt = fp.readlines()
# print(txt)
for detail in txt: # 打開分割標准文件
# print(type(detail))
pages, write_file = detail.split() # 空格分組
# write_file, write_ext = os.path.splitext(write_file) # 用於返回文件名和擴展名元組
pdf_file = f'{write_file}.pdf'
# liststr=list(map(int, pages.split('-')))
# print(type(liststr))
start_page, end_page = list(map(int, pages.split('-'))) # 將字符串數組轉換成整形數組
start_page -= 1
try:
print(f'開始分割{start_page}頁-{end_page}頁,保存為{pdf_file}......')
pdf_output = PdfFileWriter() # 實例一個 PDF文件編寫器
for i in range(start_page, end_page):
pdf_output.addPage(pdf_input.getPage(i))
with open(pdf_file, 'wb') as sub_fp:
pdf_output.write(sub_fp)
print(f'完成分割{start_page}頁-{end_page}頁,保存為{pdf_file}!')
except IndexError:
print(f'分割頁數超過了PDF的頁數')
# fp.close()
except Exception as e:
print(e)
finally:
fp_read_file.close()
# def main():
# fire.Fire(split_pdf)
#
# if __name__ == '__main__':
# main()
split_pdf('建龍微納_銀行流水_中國工商銀行_2018_112頁.pdf', '10_20_50.txt')