相关路径:菜鸟pandas
TXT提取关键信息
import os
import time
import pandas
import pdfplumber
import xlwt
import pandas as pd
import re
class OperationPdf:
def __init__(self):
self.pdf = pdfplumber.open("./data/ya.pdf")
def write_txt(self):
if os.path.exists('./pdf.txt'):
os.remove('./pdf.txt')
size = len(self.pdf.pages)
for i in range(size):
first_page = self.pdf.pages[i]
self.table = first_page.extract_text()
try:
self.fileobj = open('./pdf.txt', 'a+', encoding='utf-8')
self.fileobj.write(self.table)
# print(self.table)
except IOError:
print('写入程序中断')
self.fileobj.close()
def readtxt(self):
# 读txt文件把每行数据输出为列表
fileobj = open("./pdf-.txt", "r", encoding="utf-8")
dataMat = []
for line in fileobj.readlines():
# print(line)
curLine = line.strip().split(" ")
# floatLine=list(map(float,curLine))#这里使用的是map函数直接把数据转化成为float类型
dataMat.append(curLine[:])
disct = {}
print('dataMat:', dataMat)
return dataMat
def reade(self):
file = self.fileobj.read()
res = re.findall('(Postman)、(Charles)、(Fiddler)、(禅道)、(Testlink)、(Tapd)、(JIRA)', file)
if res == []:
pass
else:
print(res)
def saveas(self):
# 切取想要的txt中的数据另存为一个文件
fi = open("./pdf.txt", "r", encoding="utf-8")
fo = open("./pdf-.txt", "w", encoding="utf-8")
wflag = False
newline = []
dataMat = []
for line in fi: # 按行读入文件,此时line的type是str
if "陈 雅" in line: # 重置写标记
wflag = False
if "自我描述" in line: # 检验是否到了要写入的内容
wflag = True
continue
if "工作经历" in line: # 检验是否到了要写入的内容
wflag = False
if wflag == True:
K = list(line)
if len(K) > 1: # 去除文本中的空行
for i in K: # 写入需要内容
newline.append(i)
strlist = "".join(newline)
newlines = str(strlist)
D = '·'
newlines = newlines.replace(f"{D}", "") # 删掉句中·
fo.write(newlines)
fo.close()
fi.close()
if __name__ == '__main__':
count = 0
j = 0
OperationPdf().saveas()
table = OperationPdf().readtxt()
df = pd.DataFrame(table)
writer = pd.ExcelWriter('./pdf.xlsx')
if count == 0:
df.to_excel(writer, header=False, index=False, startrow=count)
else:
df[1:].to_excel(writer, header=False, index=False, startrow=count - j)
j += 1 # 避免出现空行
# time.sleep(3)
# pdfdebug().readtxt()



