安裝pypdf2:
pip install pypdf2
常見使用:
from PyPDF2 import PdfFileReader
def get_info(pdf_path):
with open(pdf_path, 'rb') as f:
pdf = PdfFileReader(f)
information = pdf.getDocumentInfo()
number_of_pages = pdf.getNumPages()
txt = """
Information about {}:
Author: {}
Creator: {}
Producer: {}
Subject: {}
Title: {}
Number of pages: {}
""".format(pdf_path, information.author, information.creator, information.producer, information.subject, information.title, number_of_pages)
print(txt)
return information
if __name__ == '__main__':
# 要注意的是如果路徑有中文字符,一般是不行的酿箭,會報錯
path = 'xxxx.pdf'
get_info(path)