Using Python to split PDF files

Source: Internet
Author: User
This article mainly introduces how to split a PDF file in Python, and how to split a PDF file into a specified number of copies. It involves the use of the pyPdf module, for more information about how to use Python to split PDF files, see the following example. Share it with you for your reference. The details are as follows:

Use pyPdf to process PDF files
Split PDF files

Usage:
1) Place the file to be split in the input_dir directory.
2. Set the number of parts to be split in the configure.txt file (if you want to split four parts, set part_num = 4)
3) execute the program
4) The split file is saved in the output_dir directory.
The 5th runtime log is written in pp_log.txt.

P.S. This program can cut multiple PDF files in batches

from pyPdf import PdfFileWriter, PdfFileReaderimport osimport timeimport sysdef part_pdf(input_file, output_file, config_count, f_w, now, file_name):  file1 = file(input_file, 'rb')  pdf = PdfFileReader(file1)  pdf_pages_len = len(pdf.pages)  if config_count <= pdf_pages_len:    ye = pdf_pages_len / config_count    lst_ye = pdf_pages_len % config_count    part_count = 0    part_count_ye = 0    for fen in range(config_count):      part_count += 1      if part_count == config_count:        part_ye = ye + lst_ye      else:        part_ye = ye      write_pdf(pdf, part_count_ye, part_count_ye+part_ye, fen, output_file)      part_count_ye += ye  else:    f_w.writelines('time: '+now+' file name: '+file_name+' status: part_num > pdf pages [error]\n')    sys.exit(1)def write_pdf(pdf, part_count_ye, part_count_ye_end, fen, output_file):  out = PdfFileWriter()  for pp in range(part_count_ye, part_count_ye_end):    out.addPage(pdf.getPage(pp))  ous = file(output_file+'_'+str(fen+1)+'.pdf', 'wb')  out.write(ous)  ous.close()def pdf_main():  f = open('configure.txt', 'r')  f_w = open('pp_log.txt', 'a')  now = time.strftime('%Y-%m-%d %H:%M:%S')  for i in f:    i_ = i.strip()    aa = i_.split('=')[1]    if i_.find('part_num=') != -1 and aa.isdigit():      config_count = int(aa)    else:      f_w.writelines('time: '+now+' status: part_num in configure.txt is error [error]\n')      sys.exit(1)  files = os.listdir('input_dir/')  for each in files:    input_file = 'input_dir/'+each    file_name = input_file[input_file.index('/'):input_file.index('.')]    output_file = 'output_dir/'+file_name    part_pdf(input_file, output_file, config_count, f_w, now, file_name)    f_w.writelines('time: '+now+' file name: '+file_name+' status: success\n')pdf_main()

I hope this article will help you with Python programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.