def open_page_web(): flag = check_new_path() if flag: try: page_num_str = str(lb.get(lb.curselection())) except Exception: alerm_msg(warning_title, warning_chose_page_msg) else: global check_dirpath check_dirpath = os.path.join(get_dirpath(new_filepath), 'web_check') if not os.path.exists(check_dirpath): os.mkdir(check_dirpath) check_file_path = os.path.join(check_dirpath, page_num_str + '.pdf') try: pdf_reader = pdfreader(new_filepath) pdf_writer = pdfwriter() pdf_writer.addPage(pdf_reader.getPage(int(page_num_str) - 1)) with open(check_file_path, 'wb') as fw: pdf_writer.write(fw) except PdfReadError: alerm_msg(error_title, error_file_msg) else: add_web_prefix_file = WEB_TITLE + check_file_path new = 0 webbrowser.open(add_web_prefix_file, new=new) elif origin_filepath != '': alerm_msg(warning_title, warning_safe_msg)
def rotate_page(rotate_pages, rotate_direction, file_path): if rotate_direction == '90': rotate_degree = 90 elif rotate_direction == '270': rotate_degree = 270 elif rotate_direction == '180': rotate_degree = 180 else: print('请输入规定内的翻转角度') try: reader_obj = pdfreader(file_path) writer_obj = pdfwriter() total_pages = reader_obj.getNumPages() for rpage in range(total_pages): if rpage + 1 in rotate_pages: page_num_obj = reader_obj.getPage(rpage).rotateClockwise( rotate_degree) else: page_num_obj = reader_obj.getPage(rpage) writer_obj.addPage(page_num_obj) with open(file_path, 'wb') as fw: writer_obj.write(fw) except PdfReadError: alerm_msg(error_title, error_file_msg) return 'file_error' else: alerm_msg(reminder_title, reminder_rotate_finish_msg)
def pdf_add_bookmark(self, obj, pdf_names): reader_obj = pdfreader(obj.file_name) pdf_writer = pdfwriter() pdf_writer.cloneDocumentFromReader(reader_obj) for i, v in enumerate(pdf_names): pdf_writer.addBookmark(u'' + v.split('\\')[-1], i) with open(obj.file_name, 'wb') as font: pdf_writer.write(font)
def set_pdf_pages(filepath): if filepath != '': page_nums = '__' try: page_nums = pdfreader(filepath).getNumPages() except PdfReadError: page_nums = 0 finally: pagetextvar.set('共 ' + str(page_nums) + ' 页') if page_nums == 0: alerm_msg(warning_title, warning_file_msg) if page_nums > 0: global total_page_nums total_page_nums = page_nums
##In the following line of codes, we are opening the "meetingminutes.pdf" pdf file and ##referring to the file as "myfile", and this file is being opened using read binary method ##because a pdf document has a binary format. ##"myfile" specifically, represents the bytes of information included in the ##"meetingminutes.pdf" pdf document. with open(r'C:\Users\bilal\Desktop\Python PDF Tutorial\meetingminutes.pdf', 'rb') as myfile: ##Code Explainer: ##In the following line of code, we are passing the information stored into the "myfile" variable into ##the pdfreader class. This pdfreader class will then return a pdfreaderobject, upon which we will be ##able to perform various functions. ##In simple words, this pdfreaderobject represents this "meetingminutes.pdf" pdfreaderobject = pdfreader(myfile) ## Number_of_pages = pdfreaderobject.getNumPages() print(f'The number of pages are\n: \n{Number_of_pages}') global_information = pdfreaderobject.getDocumentInfo() print(f""" The author of this pdf is: {global_information.author} The creator of this pdf is: {global_information.creator} The producer of this pdf is: {global_information.producer} The subject of this pdf is: {global_information.subject} The title of this pdf is: {global_information.title} """) print(f"This pdf is encrypted: {pdfreaderobject.isEncrypted}")
from PyPDF2 import PdfFileReader as pdfreader from PyPDF2 import PdfFileWriter as pdfwriter #### ####Code Explainer: ####This is creating a pdfwriter object which is going to represent the new pdf document which we are ####trying to create. We are calling this pdfwriter object as pdfWriter in our code below. ## pdfwriterobject = pdfwriter() #### ####Code Explainer: ####Opening up the file pdf document in read binary mode and then passing the read_1 stream into the ####pdfreader reader class to return the pdf. ## with open(r'C:\Users\bilal\Desktop\Python PDF Tutorial\meetingminutes.pdf', 'rb') as read_1: pdfreaderobject = pdfreader(read_1) Number_of_pages = pdfreaderobject.getNumPages() for i in range(Number_of_pages): page = pdfreaderobject.getPage(i) ## ####Code Explainer: ####In this line of code, we are essentially (for each iteration) adding the page object that we have ####created in the line above and using the addPage method to add that page into our pdfwriterobject. ####Since we are iterating through each page in our initial pdf document, we are basically adding ####each page in that document into this pdfwriterobject. #### pdfwriterobject.addPage(page) ## ####Code Explainer: ####In these lines of code, we are creating a new pdf document called "part3.pdf" and opening it up in ####the "wb" mode(or in the other words, write binary mode.)