让我们考虑一下桌面上的文件夹(Mandar和html)。现在粘贴任何pdf文件,并将其命名为'dell‘在'html’文件夹中,并在'Mandar‘文件夹中创建demo.py文件。现在,根据您的意愿创建一些txt文件(2-4),这样'html‘文件夹包含一些txt文件和一个pdf文件。
import os
import PyPDF2 # install via 'pip install PyPDF2'
# Put location of your pdf file i.e. dell.pdf in 'location' variable
location = "C:/Users/Desktop/html/"
n = "dell.pdf"
path = os.path.join(location, n)
reader = PyPDF2.PdfReader(path)
pages = len(reader.pages)
print(f"The no. of pages in {n} is {pages}.")现在运行程序,您将看到“dell.pdf中的页面编号是NUM.”//NUM是否的。你的pdf
现在让我们考虑一下'html‘文件夹总是只包含一个pdf文件与任何名字,可能是戴尔,也许ecc,也许任何名称。我希望变量'n‘存储这个pdf文件本身作为输入,以便程序将运行和显示相同的结果与不同的pdf文件名和Num。
发布于 2022-06-17 14:45:02
给标准库中的glob一个机会。它将为您提供该目录中所有匹配的PDF文件的列表。
import os
import PyPDF2
...
import glob
Location='C:/Users/Desktop/html/'
candidates = glob.glob(os.path.join(Location, '*.pdf'))
if len(candidates) == 0:
raise Exception('No PDFs found')
File=open(candidates[0],'rb')
...发布于 2022-06-18 07:31:05
你在找球拍。你可以用pathlib来做
from pathlib import Path
root = Path(location)
pdf_files = root.glob("*.pdf")https://stackoverflow.com/questions/72657525
复制相似问题