PyPDF2是Python中用于对PDF操作的第三方库,提供了删除、合并、裁剪、转换等操作 最主要有四个类: The PdfFileReader Class The PdfFileMerger Class The PageObject Class The PdfFileWriter Class
打开命令行键入
pip install PyPDF2Parameters: stream – A File object or an object that supports the standard read and seek methods similar to a File object. Could also be a string representing a path to a PDF file.
strict (bool) – Determines whether user should be warned of all problems and also causes some correctable problems to be fatal. Defaults to True.
warndest – Destination for logging warnings (defaults to sys.stderr).
overwriteWarnings (bool) – Determines whether to override Python’s warnings.py module with a custom implementation (defaults to True).
Calculates the number of pages in this PDF file.
Returns: number of pages Return type: int Raises PdfReadError: if file is encrypted and restrictions prevent this action.
Retrieves a page by number from this PDF file.
Parameters: pageNumber (int) – The page number to retrieve (pages begin at zero) Returns: a PageObject instance. Return type: PageObject
class PyPDF2.PdfFileWriter This class supports writing PDF files out, given pages produced by another class (typically PdfFileReader).
Adds a page to this PDF file. The page is usually acquired from a PdfFileReader instance.
Parameters: page (PageObject) – The page to add to the document. Should be an instance of PageObject
Writes the collection of pages added to this object out as a PDF file.
Parameters: stream – An object to write the file to. The object must support the write method and the tell method, similar to a file object.
Initializes a PdfFileMerger object. PdfFileMerger merges multiple PDFs into a single PDF. It can concatenate, slice, insert, or any combination of the above. 初始化一个PdfFileMerger对象,PdfFileMerger 用来将多个PDF合并为一个PDF,它能够连接,切割,插入或者以上的任意组合
See the functions merge() (or append()) and write() for usage information.
Parameters: strict (bool) – Determines whether user should be warned of all problems and also causes some correctable problems to be fatal. Defaults to True.
Identical to the merge() method, but assumes you want to concatenate all pages onto the end of the file instead of specifying a position. 和merge()方法相同,但假定的是你想要把全部页面连接到文件的最后而不是指定位置
Parameters: fileobj – A File Object or an object that supports the standard read and seek methods similar to a File Object. Could also be a string representing a path to a PDF file. 一个文件对象(python中用open()创建的对象)或者类似文件对象的能够支持标准读取和寻找方法的对象,也可以是一个代表指向PDF文件路径的字符串 bookmark (str) – Optionally, you may specify a bookmark to be applied at the beginning of the included file by supplying the text of the bookmark. pages – can be a Page Range or a (start, stop[, step]) tuple to merge only the specified range of pages from the source document into the output document. 可以是一个页码序列或者一个(start, stop[, step])元组,用来合并指定范围的源文件页面到输出文件
import_bookmarks (bool) – You may prevent the source document’s bookmarks from being imported by specifying this as False.
在这里插入代码片Merges the pages from the given file into the output file at the specified page number. 从指定位置合并来自给定文件的页面到输出文件
Parameters: position (int) – The page number to insert this file. File will be inserted after the given number. 插入文件的页码数,将插入到给定页数的后面 0口1 口2 口3 fileobj – A File Object or an object that supports the standard read and seek methods similar to a File Object. Could also be a string representing a path to a PDF file. bookmark (str) – Optionally, you may specify a bookmark to be applied at the beginning of the included file by supplying the text of the bookmark. pages – can be a Page Range or a (start, stop[, step]) tuple to merge only the specified range of pages from the source document into the output document.
import_bookmarks (bool) – You may prevent the source document’s bookmarks from being imported by specifying this as False.
注意:position和pages均指的是下图绿色数字,pages的范围是绿色数字之间囊括的页面
write(fileobj) Writes all data that has been merged to the given output file. 将所有被合并的数据写入到给定的输出文件中
Parameters: fileobj – Output file. Can be a filename or any kind of file-like object. 输出文件,可以是一个文件名或者所有类似文件对象的对象
Reference:https://pythonhosted.org/PyPDF2/PageObject.html