我创建了一个程序,该程序将在目录中包含的所有PDF文件中搜索单词或短语。如果在给定的PDF中找到该词组,则包含该词的页面将被提取并另存为新的PDF。
该程序非常慢。我将需要运行1000多个PDF,因此使用multiprocessing / current.futures加快处理速度非常有益。但是,我似乎无法正常工作。
在下面的代码中是否有直接启用多处理的简单方法?
import PyPDF2
import re
import os
import glob
from pathlib import Path
String = input("Enter search string: ")
inputDir = Path(input("Enter path to directory containing PDFs to search: "))
outputDir = Path(input("Enter path to directory where you would like PDFs saved: "))
outputAppend = input("Text (including separator) to be appended to end of filenames (blank if none): ")
inputDir_glob = str(inputDir) + "/*.pdf"
PDFlist = sorted(glob.glob(inputDir_glob))
if not os.path.exists(str(outputDir)):
os.makedirs(str(outputDir))
for filename in PDFlist:
object = PyPDF2.PdfFileReader(filename, strict=False)
# Get number of pages in the pdf
NumPages = object.getNumPages()
# Setup the file writer
output = PyPDF2.PdfFileWriter()
# Do the search
for i in range(0, NumPages):
PageObj = object.getPage(i)
Text = PageObj.extractText()
if re.search(String, Text):
print("File: " + filename + " | " + "Page: " + str(i))
output.addPage(object.getPage(i))
outputStream = open(str(outputDir) + "/" + os.path.splitext(os.path.basename(filename))[0] + outputAppend + ".pdf", "wb")
output.write(outputStream)
outputStream.close()
答案 0 :(得分:0)
我最终弄清楚了这一点,并认为我可以分享,以防其他人遇到类似的问题。下面的解决方案比原始代码(上面发布的)要快得多:
import PyPDF2
import re
import os
import glob
from pathlib import Path
import concurrent.futures
# Enter the search term here:
String = input("Enter search string: ")
#Enter directory containing original PDFs:
inputDir = Path(input("Enter path to directory containing PDFs to search: "))
outputDir = Path(input("Enter path to directory where you would like PDFs saved: "))
outputAppend = input("Text (including separator) to be appended to end of filenames (blank if none): ")
inputDir_glob = str(inputDir) + "/*.pdf"
PDFlist = sorted(glob.glob(inputDir_glob))
if not os.path.exists(str(outputDir)):
os.makedirs(str(outputDir))
def process_file(filename):
object = PyPDF2.PdfFileReader(filename, strict=False)
NumPages = object.getNumPages()
output = PyPDF2.PdfFileWriter()
# Do the search
for i in range(0, NumPages):
PageObj = object.getPage(i)
Text = PageObj.extractText()
if re.search(String, Text):
print("File: " + filename + " | " + "Page: " + str(i))
output.addPage(object.getPage(i))
outputStream = open(str(outputDir) + "/" + os.path.splitext(os.path.basename(filename))[0] + outputAppend + ".pdf", "wb")
output.write(outputStream)
outputStream.close()
#os.rename(filename, Path(str(outputDir) + "/Originals/" + str(os.path.basename(filename))))
with concurrent.futures.ThreadPoolExecutor(max_workers=4) as executor:
result = executor.map(process_file, (PDFlist))