Python implements a simple way to split PDF files

Source: Internet
Author: User
This example describes how Python implements a simple split PDF file. Share to everyone for your reference. Specific as follows:

Rely on Pypdf to process PDF files
Slicing PDF files

How to use:
1) Place the files that will be sliced in the Input_dir directory
2) Set the number of copies to slice in the Configure.txt file (if you want to slice 4 parts, set part_num=4)
3) Execution procedure
4) After the segmented file is saved in the Output_dir directory
5) run log written in Pp_log.txt

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

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 par T_count = 0 Part_count_ye = 0 for fen in range (Config_count): Part_count + = 1 if Part_count = = Config_coun T:part_ye = ye + lst_ye else:part_ye = ye write_pdf (pdf, Part_count_ye, Part_count_ye+part_ye, F En, output_file) part_count_ye + = Ye Else:f_w.writelines (' Time: ' +now+ ' file name: ' +file_name+ ' Status:part_nu M > 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_fil E+ ' _ ' +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 = Tim E.strftime ('%y-%m-%d%h:%m:%s ') for i in f:i_ = I.strip () AA = i_.split (' = ') [1] If I_.find (' part_num= ')! =-1 A  nd 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.write Lines (' Time: ' +now+ ' file name: ' +file_name+ ' status:success\n ') Pdf_main ()

Hopefully 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.