文章目录
概述
PyPDF2是Python中用于对PDF操作的第三方库,提供了删除、合并、裁剪、转换等操作
最主要有四个类:
The PdfFileReader Class
The PdfFileMerger Class
The PageObject Class
The PdfFileWriter Class
安装
打开命令行键入
pip install PyPDF2
一、The PdfFileReader Class
PyPDF2.PdfFileReader(stream, strict=True, warndest=None, overwriteWarnings=True)
Parameters:
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).
1、getNumPages()
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.
2、getPage(pageNumber)
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
二、The PdfFileWriter Class
class PyPDF2.PdfFileWriter
This class supports writing PDF files out, given pages produced by another class (typically PdfFileReader).
1、addPage(page)
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
2、write(stream)
Writes the collection of pages added to this object out as a PDF file.
Parameters: stream – An ob