我在 lxml 檔案中找到了 bbox 坐標,并設法用 PDFQuery 提取了想要的資料。然后我將資料寫入 csv 檔案。
def pdf_scrape(pdf):
"""
Extract each relevant information individually
input: pdf to be scraped
returns: dataframe of scraped data
"""
# Define coordinates of text to be extracted
CUSTOMER = pdf.pq('LTTextLineHorizontal:overlaps_bbox("356.684, 563.285, 624.656, 580.888")').text()
CUSTOMER_REF = pdf.pq('LTTextLineHorizontal:overlaps_bbox("356.684, 534.939, 443.186, 552.542")').text()
SALES_ORDER = pdf.pq('LTTextLineHorizontal:overlaps_bbox("356.684, 504.692, 414.352, 522.295")').text()
ITEM_NUMBER = pdf.pq('LTTextLineHorizontal:overlaps_bbox("356.684, 478.246, 395.129, 495.849")').text()
KEY = '0000' SALES_ORDER '-' '00' ITEM_NUMBER
# Combine all relevant information into a single pandas dataframe
page = pd.DataFrame({
'KEY' : KEY,
'CUSTOMER' : CUSTOMER,
'CUSTOMER REF.': CUSTOMER_REF,
'SALES ORDER' : SALES_ORDER,
'ITEM NUMBER' : ITEM_NUMBER
}, index=[0])
return(page)
pdf_search = Path("files/").glob("*.pdf")
pdf_files = [str(file.absolute()) for file in pdf_search]
master = list()
for pdf_file in pdf_files:
pdf = pdfquery.PDFQuery(pdf_file)
pdf.load(0)
# Iterate over all pages in document and add scraped data to df
page = pdf_scrape(pdf)
master.append(page)
master = pd.concat(master, ignore_index=True)
master.to_csv('scraped_PDF_as_csv\scraped_PDF_DataFrame.csv', index = False)
問題是我每天需要閱讀數百個 PDF,而這個腳本需要大約 13-14 秒才能從僅 10 個 PDF 的第一頁中挖掘四個元素。
有沒有辦法加快我的代碼?我看過這個:

PyMuPDF 幾乎同時運行兩個 PDF,我認為我們看到 PDFQuery 需要更長的時間來進行這些n**2/2交叉比較。
我認為你會放棄很多便利來嘗試自己做這件事。如果您的 PDF 是一致的,您可能可以調整 PyMuPDF 并使其正確,但如果它們的創建方式存在差異,則可能需要更長的時間才能正確(即使有,因為 PDF 中的文本看似棘手)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/489399.html
