pdf数据处理写到excl
import openpyxl
import pdfplumber
if __name__ == '__main__':
excel_file = input("余老板:请输入excel对应输出地址路径: ").strip("'\"")
while True:
try:
pdf_path = input("余老板:那个输入一下pdf文件对应地址路径,全路径哦: ").strip("'\"")
structural = [0, 1, 2, 3, 4, 5, 10, 11]
other_structural = [6, 7, 8, 9, 14, 15]
rows = []
result = {}
rows_process = []
with pdfplumber.open(pdf_path) as pdf:
second_page = pdf.pages[1]
tables = second_page.extract_tables()
for table in tables:
for row in table:
rows.append(row)
for index, row in enumerate(rows):
if index in structural:
for i in range(0, len(row), 2):
if i + 1 < len(row) and row[i] is not None:
if row[i + 1] is None:
result[row[i].replace("\n", '')] = ''
else:
if i + 1 < len(rows) and rows[i + 1] is not None:
result[row[i].replace("\n", '')] = row[i + 1]
elif index in other_structural:
rows_process.append(row)
for i in range(0, len(rows_process), 2):
if i + 1 < len(rows_process):
keys = rows_process[i]
values = rows_process[i + 1]
for j in range(len(keys)):
if keys[j] is not None and j < len(values) and values[j] is not None:
if "资格取得时间" in keys[j].replace("\n", ''):
if "与评" in values[j].replace("\n", ''):
result["资格取得时间"] = values[j].replace("\n", '').replace("与评", "")
else:
values[j].replace("\n", '')
else:
result[keys[j].replace("\n", '')] = values[j].replace("\n", '')
title_data = []
with pdfplumber.open(pdf_path) as pdf:
second_page = pdf.pages[2]
tables = second_page.extract_tables()
for table in tables:
for row in table:
title_data.append(row)
education_experience = []
for row in title_data:
if row[0] == '1.教 育 经 历':
start_index = title_data.index(row) + 1
for data_row in title_data[start_index:]:
if data_row[0] == '2.工 作 经 历':
break
education_experience.append(data_row)
education_experience = [row for row in education_experience if row and any(item for item in row)]
if education_experience:
last_education = education_experience[-1]
year = last_education[0]
college = last_education[1]
degree = last_education[2]
major = last_education[4]
result["第一学历"] = degree
result["第一毕业院校"] = college
result["第一专业"] = major
result["第一毕业时间"] = year
workbook = openpyxl.load_workbook(excel_file)
sheet = workbook['Sheet1']
last_row = sheet.max_row
header_mapping = {
'序号': last_row - 1,
'姓名': result['姓名'],
'性别': result['性别'],
'行业': None,
'身份证件号码': result['身份证件号码'],
'出生地': result['出生地'],
'出生日期': result['出生日期'],
'政治面貌': result['政治面貌'],
'现从事专业及时间': result['现从事专业及时间'],
'参加工作时间': result['参加工作时间'],
'现工作单位': result['现工作单位'],
}
for col_num, (header, value) in enumerate(header_mapping.items(), start=1):
sheet.cell(row=last_row + 1, column=col_num, value=value)
workbook.save(excel_file)
cont = input("余老板 : 是否继续输入啊?回车继续哈 n 就结束啦: ")
if cont.lower() == 'n':
break
except Exception as e:
print("这条数据发生错误/找一下寿春同学:", str(e))
pip install pyinstaller
pyinstaller -F xxx -i xxx -n 脚本