1. PDF File Handle with pypdf2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 | from PyPDF2 import PdfFileWriter, PdfFileReader def get_info(path): with open(path, 'rb') as f: pdf = PdfFileReader(f) info = pdf.getDocumentInfo() number_of_pages = pdf.getNumPages() print(info) print('pages: ' + str(number_of_pages)) author = info.author creator = info.creator producer = info.producer subject = info.subject title = info.title def text_extractor(path): with open(path, 'rb') as f: pdf = PdfFileReader(f) # get the first page page = pdf.getPage(1) print(page) print('Page type: {}'.format(str(type(page)))) text = page.extractText() print('text: ' + text) def pdf_splitter(path): import os import glob fname = os.path.splitext(os.path.basename(path))[0] pdf = PdfFileReader(path) for page in range(pdf.getNumPages()): pdf_writer = PdfFileWriter() pdf_writer.addPage(pdf.getPage(page)) output_filename = '{}_page_{}.pdf'.format(fname, page+1) with open(output_filename, 'wb') as out: pdf_writer.write(out) print('Created: {}'.format(output_filename)) def merger(output_path, input_paths): pdf_writer = PdfFileWriter() for path in input_paths: pdf_reader = PdfFileReader(path) for page in range(pdf_reader.getNumPages()): pdf_writer.addPage(pdf_reader.getPage(page)) with open(output_path, 'wb') as fh: pdf_writer.write(fh) ''' import glob if __name__ == '__main__': paths = glob.glob('w9_*.pdf') paths.sort() merger('pdf_merger.pdf', paths) ''' def merger1(output_path, input_paths): pdf_merger = PdfFileMerger() file_handles = [] for path in input_paths: pdf_merger.append(path) with open(output_path, 'wb') as fileobj: pdf_merger.write(fileobj) ''' if __name__ == '__main__': paths = glob.glob('fw9_*.pdf') paths.sort() merger1('pdf_merger2.pdf', paths) ''' def rotator(path): pdf_writer = PdfFileWriter() pdf_reader = PdfFileReader(path) page1 = pdf_reader.getPage(0).rotateClockwise(90) pdf_writer.addPage(page1) page2 = pdf_reader.getPage(1).rotateCounterClockwise(90) pdf_writer.addPage(page2) pdf_writer.addPage(pdf_reader.getPage(2)) with open('pdf_rotator.pdf', 'wb') as fh: pdf_writer.write(fh) def watermark(input_pdf, output_pdf, watermark_pdf): watermark = PdfFileReader(watermark_pdf) watermark_page = watermark.getPage(0) pdf = PdfFileReader(input_pdf) pdf_writer = PdfFileWriter() for page in range(pdf.getNumPages()): pdf_page = pdf.getPage(page) pdf_page.mergePage(watermark_page) pdf_writer.addPage(pdf_page) with open(output_pdf, 'wb') as fh: pdf_writer.write(fh) ''' if __name__ == '__main__': watermark(input_pdf='w9.pdf', output_pdf='watermarked_w9.pdf', watermark_pdf='watermark.pdf') ''' def encrypt(input_pdf, output_pdf, password): pdf_writer = PdfFileWriter() pdf_reader = PdfFileReader(input_pdf) for page in range(pdf_reader.getNumPages()): pdf_writer.addPage(pdf_reader.getPage(page)) pdf_writer.encrypt(user_pwd=password, owner_pwd=None, use_128bit=True) with open(output_pdf, 'wb') as fh: pdf_writer.write(fh) ''' if __name__ == '__main__': encrypt(input_pdf='reportlab-sample.pdf', output_pdf='encrypted.pdf', password='blowfish') ''' if __name__ == '__main__': path = r'k:\Torrent\Download\PostView.nhn.pdf' get_info(path) text_extractor(path) pdf_splitter(path) |
댓글 없음:
댓글 쓰기