程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

python學習筆記之pdf文檔提取

編輯:Python
""" # !/usr/bin/env python # -*- coding:utf-8 -*- # @Author : 史沐凡 # @file : pdf____張坤_文檔轉換工具.py # @Time : 2022/7/24 22:21 # @Function: """
# 讀取docx中的文本代碼示例
import glob
import os
import re
from openpyxl import Workbook # 寫入的時候用
import pdfplumber
def getfields(filepath):
''' 直接讀取pdf,獲取所需要的字段 患者姓名 標識號 出生日期 性別 保險集團 膠囊標識號 操作日期 轉診醫生 登記者 預約者 轉診原因 體重 身高 腰圍 體型 通過胃部的時間 通過小腸的時間 操作信息與發現 摘要與建議 :return:list '''
print(filepath)
患者姓名 = ""
標識號 = ""
出生日期 = ""
性別 = ""
保險集團 = ""
膠囊標識號 = ""
操作日期 = ""
轉診醫生 = ""
登記者 = ""
預約者 = ""
轉診原因 = ""
體重 = ""
身高 = ""
腰圍 = ""
體型 = ""
通過胃部的時間 = ""
通過小腸的時間 = ""
操作信息與發現 = ""
摘要 = ""
建議 = ""
fields = ['患者姓名',
'標識號',
'出生日期',
'性別',
'保險集團',
'膠囊標識號',
'操作日期',
'轉診醫生',
'登記者',
'預約者',
'轉診原因',
'體重',
'身高',
'腰圍',
'體型',
'通過胃部的時間',
'通過小腸的時間',
'操作信息與發現',
'摘要',
'建議']
nr = ""
with pdfplumber.open(filepath) as pdf:
for page in pdf.pages: # 遍歷所有頁碼
text = page.extract_text() # 提取文本
nr=nr+text
nr = nr.strip()
print(nr)
res = re.search(r"患者姓名 (.*)", nr, 0)
if res: 患者姓名 = res.group(1).strip()
res = re.search(r"標識號 (.*)", nr, 0)
if res: 標識號 = res.group(1).strip()
res = re.search(r"出生日期 (.*)", nr, 0)
if res: 出生日期 = res.group(1).strip()
res = re.search(r"性別 (.*)性", nr, 0)
if res: 性別 = res.group(1).strip()
res = re.search(r"保險集團 (.*)", nr, 0)
if res: 保險集團 = res.group(1).strip()
res = re.search(r"膠囊標識號 (.*)", nr, 0)
if res: 膠囊標識號 = res.group(1).strip()
res = re.search(r"操作日期 (.*)", nr, 0)
if res: 操作日期 = res.group(1).strip()
res = re.search(r"轉診醫生 (.*)", nr, 0)
if res: 轉診醫生 = res.group(1).strip()
res = re.search(r"登記者 (.*)", nr, 0)
if res: 登記者 = res.group(1).strip()
res = re.search(r"預約者 (.*)", nr, 0)
if res: 預約者 = res.group(1).strip()
res = re.search(r"轉診原因(.*)患者數據", nr, re.DOTALL)
if res:
轉診原因 = res.group(1).strip()
res = re.search(r"體重 ?:(\d*) ?kg", nr, 0)
if res: 體重 = res.group(1).strip()
res = re.search(r"身高 ?:(\d*) ?cm", nr, 0)
if res: 身高 = res.group(1).strip()
res = re.search(r"腰圍 ?:(\d*) ?cm", nr, 0)
if res: 腰圍 = res.group(1).strip()
res = re.search(r"體型 ?:(.{1,3}),", nr, 0)
if res: 體型 = res.group(1).strip().strip()
res = re.search(r"通過胃部的時間 ?:(.*),", nr, 0)
if res: 通過胃部的時間 = res.group(1).strip()
res = re.search(r"通過小腸的時間 ?:(.*)", nr, 0)
if res: 通過小腸的時間 = res.group(1).strip()
res = re.search(r"操作信息與發現(.*)摘要與建議", nr, re.DOTALL)
if res:
操作信息與發現 = res.group(1).strip()
操作信息與發現 = 操作信息與發現.replace(" ", "")
ls = 操作信息與發現.split("\n")
操作信息與發現 = ";".join(ls).strip()
res = re.search(r"摘要與建議(.*)建議:", nr, re.DOTALL)
if res:
摘要 = res.group(1).strip()
摘要 = 摘要.replace(" ", "")
ls = 摘要.split("\n")
摘要 = ";".join(ls).strip()
res = re.search(r"建議:(.*)簽名", nr, re.DOTALL)
if res:
建議 = res.group(1).strip()
建議 = 建議.replace(" ", "")
ls = 建議.split("\n")
建議 = ";".join(ls).strip()
print("*" * 88)
ls = [患者姓名, 標識號, 出生日期, 性別,
保險集團, 膠囊標識號, 操作日期, 轉診醫生,
登記者, 預約者, 轉診原因, 體重,
身高, 腰圍, 體型, 通過胃部的時間,
通過小腸的時間, 操作信息與發現, 摘要,建議]
print(ls)
if (all(ls)):
return ls
else:
return ls
if __name__ == '__main__':
fields = ['患者姓名', '標識號', '出生日期', '性別', '保險集團', '膠囊標識號', '操作日期', '轉診醫生', '登記者', '預約者', '轉診原因',
'體重', '身高', '腰圍',
'體型', '通過胃部的時間', '通過小腸的時間', '操作信息與發現', '摘要','建議']
print(fields)
root = os.getcwd()
ls = glob.glob("pdf報告\*.pdf")
big_ls = []
for i in ls:
i=os.path.join(os.getcwd(),i)
ls = getfields(i)
big_ls.append(ls)
for i in big_ls: print(i)
wb = Workbook()
ws = wb.active
ws.append(fields)
for i in big_ls:
ws.append(i)
wb.save(r'pdf報告/pdf轉換表格.xlsx')
exit(0)

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved