作品名稱:自動提取PDF表格數(shù)據(jù)并保存為Excel文件開發(fā)環(huán)境:PyCharm 2023.3.4 + python3.7用到的庫:pdfplumber、pandas、logging(用于日志記錄的一個強大工具)作品簡介:該實例使用pdfplumber庫來提取PDF文件中的表格數(shù)據(jù),并使用pandas庫將這些數(shù)據(jù)保存為Excel文件。此外,代碼還使用了logging庫來記錄操作過程中的信息和錯誤。

以下是代碼的詳細(xì)說明:
"""提取pdf的表格數(shù)據(jù)保存為Excel文件"""import pdfplumberimport pandas as pdimport logging
logging.basicConfig(level=logging.INFO)
# PDF文件路徑pdf_path = 'leisure.pdf'
# 使用pdfplumber打開PDF文件try: with pdfplumber.open(pdf_path) as pdf: all_dfs = []
for page in pdf.pages: tables = page.extract_tables()
for table in tables: df = pd.DataFrame(table[1:], columns=table[0]) all_dfs.append(df)
combined_df = pd.concat(all_dfs, ignore_index=True)
excel_path = "table_from_page.xlsx" combined_df.to_excel(excel_path, index=False)
logging.info(f"表格數(shù)據(jù)已保存為Excel文件:{excel_path}")
except Exception as e: logging.error(f"處理PDF或保存Excel時發(fā)生錯誤:{str(e)}")
閱讀原文:原文鏈接
該文章在 2025/8/28 15:45:50 編輯過