45 KiB
45 KiB
In [ ]:
import os,sys,shutil
import openpyxl
import math
fi_xls = os.getcwd() + '/data/高新区.xlsx'
fi_path = os.getcwd() + '/file/220720'
old = []
dict1 = {}
wb = openpyxl.load_workbook(fi_xls)
sheet = wb.active
depart = []
for n in range(2,sheet.max_row+1):
m_name = str(sheet.cell(n,1).value)
m_depart = sheet.cell(n,5).value
if m_depart not in depart:
depart.append(sheet.cell(n,5).value)
dict1[int(m_name.split('.')[0])] = [sheet.cell(n,2).value.strip(),sheet.cell(n,5).value.strip()]
# 创建部门办公室
m_path = os.getcwd()+'/file/220427/new'
for pn in depart:
if not os.path.exists(m_path + '/' + pn):
os.mkdir(m_path + '/' + pn)
fl=os.listdir(fi_path)
for fn in fl:
if os.path.isfile(fi_path + '/' + fn):
ofn = int(fn.split('.')[0])
old.append(ofn)
old.sort()
for n in old:
o_name = f'{fi_path}/{n}.pdf'
n_name = f'{fi_path}/new/{dict1[n][1]}/{str(n).rjust(5,"0")}-{dict1[n][0]}.pdf'
if not os.path.exists(n_name):
shutil.copyfile(o_name,n_name)
print(n_name)
#print(dict1)In [3]:
import os,sys,shutil
import json
import glob
from pathlib import Path
filepath = 'file/line/'
new = 'file/Line1/'
files = glob.glob(f'{filepath}*.SAC')
for fn in files:
fi_name =Path(fn).stem.split('_')[0]
n_name = Path(new,fi_name+'.SAC')
shutil.copyfile(fn,n_name)
In [3]:
import os,sys,shutil
from pathlib import Path
fi_path = Path('file/北海/2022年')
new_path = 'file/北海/new/2022年'
pdf_files = list(fi_path.glob('**/*.pdf'))
fls = list(fi_path.glob('**/*.pdf'))
for fn in fls:
fi_name =Path(fn).name
n_name = Path(new_path,fi_name)
shutil.copyfile(fn,n_name)In [ ]:
import os,sys,shutil
import openpyxl
import math
fi_xls = os.getcwd() + '/data/高新区.xlsx'
fi_path = os.getcwd() + '/file/220720'
old = []
dict1 = {}
wb = openpyxl.load_workbook(fi_xls)
sheet = wb.active
depart = []
for n in range(2,sheet.max_row+1):
m_name = str(sheet.cell(n,1).value)
m_depart = sheet.cell(n,5).value
if m_depart not in depart:
depart.append(sheet.cell(n,5).value)
dict1[sheet.cell(n,4).value] = [sheet.cell(n,6).value,sheet.cell(n,3).value]
#print(dict1)
fl=os.listdir(fi_path)
for fn in fl:
if os.path.isfile(fi_path + '/' + fn):
ofn = int(fn.split('.')[0])
old.append(ofn)
old.sort()
wb = openpyxl.Workbook()
sheet = wb.active
i = 0
for item in old:
m_bh = item
m_name = dict1[item][0]
m_dep = dict1[item][1]
i += 1
sheet[f'A{i}'] = m_bh
sheet[f'B{i}'] = m_name
sheet[f'C{i}'] = m_dep
wb.save('data/高新区报告.xlsx') In [ ]:
import os,sys,shutil
import openpyxl
import math
fi_xls = os.getcwd() + '/data/北京低碳监测.xlsx'
fi_path = os.getcwd() + '/file/220724'
old = []
dict1 = {}
wb = openpyxl.load_workbook(fi_xls)
sheet = wb.active
depart = []
for n in range(2,sheet.max_row):
m_name = sheet.cell(n,6).value
m_depart = sheet.cell(n,3).value
if sheet.cell(n,4).value is not None:
dict1[int(sheet.cell(n,4).value)] = [m_name,m_depart]
m_path = os.getcwd()+'/file/220724/new'
fl=os.listdir(fi_path)
for fn in fl:
if os.path.isfile(fi_path + '/' + fn):
ofn = int(fn.split('.')[0])
old.append(ofn)
old.sort()
for n in old:
o_name = f'{fi_path}/{n}.pdf'
n_name = f'{fi_path}/new/{str(n).rjust(5,"0")}-{dict1[n][0]}.pdf'
if not os.path.exists(n_name):
shutil.copyfile(o_name,n_name)
print(n_name)
#print(dict1)In [ ]:
import os,sys,shutil
import openpyxl
import math
import json
fi_xls = os.getcwd() + '/data/北京低碳监测.xlsx'
fi_path = os.getcwd() + '/file/220724'
old = []
dict1 = {}
dict2 = {}
dict3 = {}
wb = openpyxl.load_workbook(fi_xls)
sheet = wb.active
depart = []
for n in range(2,sheet.max_row+1):
m_name = sheet.cell(n,6).value
m_depart = sheet.cell(n,3).value
if sheet.cell(n,4).value is not None:
dict1[int(sheet.cell(n,4).value)] = [m_name,m_depart]
fi_xls = os.getcwd() + '/data/1658883945637.xlsx'
wb = openpyxl.load_workbook(fi_xls)
sheet = wb.active
for n in range(2,sheet.max_row+1):
m_dm = sheet.cell(n,3).value
m_depart = sheet.cell(n,2).value
m_name = sheet.cell(n,4).value
if sheet.cell(n,3).value is not None:
dict2[sheet.cell(n,3).value] = [m_name,m_depart,m_dm]
#print(dict2)
fl=os.listdir(fi_path)
for fn in fl:
if os.path.isfile(fi_path + '/' + fn):
ofn = int(fn.split('.')[0])
old.append(ofn)
old.sort()
for n in old:
m_name = dict1[n][0]
m_depart = dict1[n][1]
for item in dict2.values():
if m_name in item and m_depart in item:
dict3[n] = [m_name,m_depart,item[2]]
break
for k in old:
if k not in dict3.keys():
print(k)
with open('data/低碳院报告.json','w') as fl2:
json.dump(dict3,fl2,ensure_ascii=False) In [ ]:
import json
import os
fl_name = 'data/低碳院报告.json'
with open(fl_name,'r') as fl:
m_xx = json.load(fl)
for k, v in m_xx.items():
m_bh = str(k).rjust(5,"0")
fl_name = f'file/220724/new/{m_bh}-{v[0]}.pdf'
if os.path.exists(fl_name):
print(fl_name)
else:
print(f'{v[0]}不存在')In [ ]:
import json
import openpyxl
fl_name = 'data/低碳院报告.json'
with open(fl_name,'r') as fl:
m_xx = json.load(fl)
wb = openpyxl.Workbook()
sheet = wb.active
i = 0
for k, v in m_xx.items():
m_bh = str(k).rjust(5,"0")
m_name = v[0]
fl = f'{m_bh}-{v[0]}.pdf'
m_rec = v[2]+'@ceic.com'
i += 1
sheet[f'A{i}'] = i
sheet[f'B{i}'] = m_bh
sheet[f'C{i}'] = m_name
sheet[f'D{i}'] = m_rec
sheet[f'E{i}'] = fl
wb.save('data/低碳院邮件.xlsx')
In [ ]:
import glob
import os,shutil
filepath = 'file/yimo/'
old = 'SGF'
new = 'sgf'
files = glob.glob(f'{filepath}*.{old}')
for f in files:
new_file =os.path.splitext(f)[0]+'.'+new
os.rename(f,new_file)In [ ]:
import fitz
import os
def covert2pic(zoom):
if os.path.exists('.pdf'): # 临时文件,需为空
os.removedirs('.pdf')
os.mkdir('.pdf')
for pg in range(totaling):
page = doc[pg]
zoom = int(zoom) #值越大,分辨率越高,文件越清晰
rotate = int(0)
print(page)
trans = fitz.Matrix(zoom / 100.0, zoom / 100.0).preRotate(rotate)
pm = page.getPixmap(matrix=trans, alpha=False)
lurl='.pdf/%s.jpg' % str(pg+1)
pm.writePNG(lurl)
doc.close()
def pic2pdf(obj):
doc = fitz.open()
for pg in range(totaling):
img = '.pdf/%s.jpg' % str(pg+1)
imgdoc = fitz.open(img) # 打开图片
pdfbytes = imgdoc.convertToPDF() # 使用图片创建单页的 PDF
os.remove(img)
imgpdf = fitz.open("pdf", pdfbytes)
doc.insertPDF(imgpdf) # 将当前页插入文档
if os.path.exists(obj): # 若文件存在先删除
os.remove(obj)
doc.save(obj) # 保存pdf文件
doc.close()
def pdfz(sor, obj, zoom):
covert2pic(zoom)
pic2pdf(obj)
sor = "5.pdf" # 需要压缩的PDF文件
obj = "new-" + sor
doc = fitz.open(sor)
totaling = doc.pageCount
zoom = 150 # 清晰度调节,缩放比率
pdfz(sor, obj, zoom)
os.removedirs('.pdf')
In [ ]:
from pdf2image import convert_from_path, convert_from_bytes
import os,sys
import tempfile
from pdf2image.exceptions import (
PDFInfoNotInstalledError,
PDFPageCountError,
PDFSyntaxError
)
#images = convert_from_path('1.pdf',dpi=200,fmt='jpg', output_folder='./sample_data')
with tempfile.TemporaryDirectory() as path:
images_from_path = convert_from_path('检查.pdf', dpi=100,fmt='jpg', output_folder='./pic')
print(path)In [ ]:
import img2pdf
import os,sys
import glob
fl=glob.glob('./pic/*.jpg')
fl.sort()
a4inpt = (img2pdf.mm_to_pt(210),img2pdf.mm_to_pt(297))
layout_fun = img2pdf.get_layout_fun(a4inpt)
with open("name.pdf","wb") as f:
f.write(img2pdf.convert(fl,layout_fun=layout_fun))In [ ]:
import glob
import fitz # 导入本模块需安装pymupdf库
import os,sys
def pic2pdf_1(img_path, pdf_path, pdf_name):
doc = fitz.open()
fl=os.listdir(img_path)
fl.sort()
width, height = fitz.PaperSize("a4")
#width, height = fitz.paper_size("a4")
for img in fl:
fn = img_path+'/'+img
if os.path.isfile(fn):
imgdoc = fitz.open(img_path+'/'+img)
pdfbytes = imgdoc.convertToPDF()
imgpdf = fitz.open("pdf", pdfbytes,width = width, height = height)
doc.insertPDF(imgpdf)
doc.save(pdf_path +'/'+ pdf_name)
doc.close()
img_path = os.getcwd() +'/pic'
pdf_path = os.getcwd()
pic2pdf_1(img_path=img_path, pdf_path=pdf_path, pdf_name='1.pdf')In [ ]:
#将指定目录下图片文件进行文字识别
import os,sys
from aip import AipOcr
import glob
""" 你的 APPID AK SK """
APP_ID = '17553946'
API_KEY = 'IMC1ss3Tyo3vEAdVH6jgcdv2'
SECRET_KEY = 'Iua6KdhuDtz159rjzGeFGpqgjhnoU0zZ'
client = AipOcr(APP_ID, API_KEY, SECRET_KEY)
def get_file_content(filePath):
with open(filePath, 'rb') as fp:
return fp.read()
options = {}
options["language_type"] = "CHN_ENG"
options["detect_direction"] = "true"
options["detect_language"] = "true"
options["probability"] = "true"
fi_path = os.getcwd()+'/pic'
fl = glob.glob(f'{fi_path}/*.jpg')
fl.sort()
for fl1 in fl:
file_name = fl1
image = get_file_content(file_name)
result= client.basicGeneral(image, options)
if 'words_result' in result:
print('\n'.join([w['words'] for w in result['words_result']]))
print('\n')
In [ ]:
In [ ]:
In [ ]:
import docx
import json
Doc = docx.Document(r"症状对症处方选穴.docx")
#print("檔案內含段落數:",len(Doc.paragraphs),"\n")
testList = []
text1 = []
dict1 = {}
for text in Doc.paragraphs:
testList.append(text)
i = 0
for pg in testList:
if len(pg.text) >0:
s = ''.join(pg.text.split())
if s[0:1] == '第':
i += 1
n = 0
dict1.setdefault(i,{})
dict1[i]['title'] = pg.text.split()[1]
dict1[i]['sub'] = {}
#dict1[i]['title'].setdefault(i,{})
elif s[0:1] == '(':
sub = s.split(')')[1]
dict2 = {}
dict1[i]['sub'].setdefault(sub,[])
else:
dict1[i]['sub'][sub].append(s)
filename = '症状对症处方选穴.json'
with open(filename,'w') as fl:
json.dump(dict1, fl,ensure_ascii=False)
print('ok') In [ ]:
import docx
import json
Doc = docx.Document(r"常见疾病辨病处方选穴.docx")
#print("檔案內含段落數:",len(Doc.paragraphs),"\n")
testList = []
text1 = []
dict1 = {}
for text in Doc.paragraphs:
testList.append(text)
i = 0
for pg in testList:
if len(pg.text) >0:
s = ''.join(pg.text.split())
if s[0:1] == '第':
i += 1
n = 0
dict1.setdefault(i,{})
dict1[i]['title'] = pg.text.split('节')[1]
dict1[i]['sub'] = {}
#dict1[i]['title'].setdefault(i,{})
elif s[0:1] == '(':
sub = s.split(')')[1]
dict2 = {}
dict1[i]['sub'].setdefault(sub,[])
else:
dict1[i]['sub'][sub].append(s)
filename = '常见疾病辨病处方选穴.json'
with open(filename,'w') as fl:
json.dump(dict1, fl,ensure_ascii=False)
print('ok') In [ ]:
import json
import re
filename = '症状对症处方选穴.json'
pattern = r'[\d\.]'
mo =r'[\u4e00-\u9fa5]+'
dict2 = {}
with open(filename,'r') as fl:
dict1 = json.load(fl)
for k,v in dict1.items():
m_title = v['title']
for k1,v1 in v['sub'].items():
sub_title = k1
s = ''
for mx in v1:
s = s + mx
list1 = re.split(pattern, s)
list2 = []
for ss in list1:
if ss != '':
list3 = re.findall(mo,ss)
list2.append(list3)
#print(m_title,sub_title,list2)
dict2.setdefault(m_title,{})
dict2[m_title][sub_title] = list2
filename = 'new_症状对症处方选穴.json'
with open(filename,'w') as fl:
json.dump(dict2, fl,ensure_ascii=False)
print('ok')
In [ ]:
import openpyxl
from docxtpl import DocxTemplate
wb = openpyxl.load_workbook('file/火车餐厅人员信息表1.xlsx')
sheet = wb.active
# sheets = wb.sheetnames
list1 = []
context = {}
for n in range(2, sheet.max_row+1):
dict1 = {}
dict1['xh'] = sheet.cell(n,1).value
dict1['xm'] = sheet.cell(n,2).value
dict1['sfzhm'] = sheet.cell(n,3).value
dict1['dh'] = sheet.cell(n,4).value
dict1['cx'] = sheet.cell(n,5).value
dict1['xzdz'] = sheet.cell(n,6).value
list1.append(dict1)
context['info'] = list1
tpl = DocxTemplate("file/火车餐厅人员信息表.docx")
tpl.render(context)
tpl.save('file/test1.docx')In [ ]:
from newpdfminer.pdfparser import PDFParser, PDFDocument
from newpdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter
from newpdfminer.converter import PDFPageAggregator
from newpdfminer.layout import LAParams, LTTextBox
from newpdfminer.pdfinterp import PDFTextExtractionNotAllowed
path = "file/211027/114.pdf"
# 用文件对象来创建一个pdf文档分析器
praser = PDFParser(open(path, 'rb'))
# 创建一个PDF文档
doc = PDFDocument()
# 连接分析器 与文档对象
praser.set_document(doc)
doc.set_parser(praser)
# 提供初始化密码
# 如果没有密码 就创建一个空的字符串
doc.initialize()
# 检测文档是否提供txt转换,不提供就忽略
if not doc.is_extractable:
raise PDFTextExtractionNotAllowed
else:
# 创建PDf 资源管理器 来管理共享资源
rsrcmgr = PDFResourceManager()
# 创建一个PDF设备对象
laparams = LAParams()
device = PDFPageAggregator(rsrcmgr, laparams=laparams)
# 创建一个PDF解释器对象
interpreter = PDFPageInterpreter(rsrcmgr, device)
# 循环遍历列表,每次处理一个page的内容
for page in doc.get_pages():
interpreter.process_page(page)
# 接受该页面的LTPage对象
layout = device.get_result()
# 这里layout是一个LTPage对象,里面存放着这个 page 解析出的各种对象
# 包括 LTTextBox, LTFigure, LTImage, LTTextBoxHorizontal 等
for x in layout:
if isinstance(x, LTTextBox):
print(x.get_text().strip())In [ ]:
import pdfplumber
from PIL import Image
# 读取 PDF 文档
pdf = pdfplumber.open("5.pdf")
# 获取页数
print("总页数:",len(pdf.pages))
print("-----------------------------------------")
# 读取第 4 页;索引从 1 开始
page = pdf.pages[0]
print("本页:",page.page_number + 1)
print("-----------------------------------------")
# 单页文件存储为图片
#im = page.to_image()
#im.save('./5_1.png', format='PNG')
# 单页文件中原始图片读取,不能读取图表文件
#imgs = page.images
#for i, img in enumerate(imgs):
# size = img['width'], img['height']
# data = img['stream'].get_data()
# out_path = f'003pdf_images_{i}.png'
# with open(out_path, 'wb') as fimg_out:
# fimg_out.write(data)
# 导出第 4 页文本
text = page.extract_text()
print(text)
In [ ]:
import fitz
import re
import os
file_path = '5.pdf' # PDF 文件路径
dir_path = './data' # 存放图片的文件夹
def pdf2image1(path, pic_path):
checkIM = r"/Subtype(?= */Image)"
pdf = fitz.open(path)
lenXREF = pdf.xref_length()
count = 1
for i in range(1, lenXREF):
text = pdf.xref_object(i)
isImage = re.search(checkIM, text)
if not isImage:
continue
pix = fitz.Pixmap(pdf, i)
new_name = f"img_{count}.png"
pix.writePNG(os.path.join(pic_path, new_name))
count += 1
pix = None
pdf2image1(file_path, dir_path)In [ ]:
import os, sys
from PyPDF2 import PdfFileWriter, PdfFileReader, PdfFileMerger
file = "file/211027/114.pdf"
pdf_reader = PdfFileReader(file)
n = pdf_reader.numPages
print(n)
pdfWriter = PdfFileWriter()
pageObj = pdf_reader.getPage(n-2)
pageObj.scaleBy(1)
pdfWriter.addPage(pageObj)
with open('split_114.pdf', 'wb') as pdfOutputFile:
pdfWriter.write(pdfOutputFile)
In [ ]:
import fitz
import os
file = "file/211027/114.pdf"
doc = fitz.open(file)
n = doc.page_count
print(n)
doc2 = fitz.open()
page = doc[n - 2]
#doc2.insert_pdf(doc, to_page = n-2) # first 10 pages
doc2.insert_pdf(doc, from_page = n-2,to_page = n-2)
#print(type(page))
#doc2.insert_pdf(page) # last 10 pages
doc2.save("first-and-last-10.pdf")
In [ ]:
import fitz
import os
import glob
fi_path = 'file/2/'
dict1 = {}
fl = glob.glob(f'{fi_path}*.pdf')
list1 = []
i = 0
doc2 = fitz.open()
for fn in fl:
doc = fitz.open(fn)
doc2.insert_pdf(doc, from_page = 1,to_page = 1)
doc2.save("检查.pdf")
In [ ]:
import fitz
import os
import glob
import img2pdf
fi_path = 'file/2/'
zoom = 100
fl = glob.glob(f'{fi_path}*.pdf')
i = 0
doc2 = fitz.open()
for fn in fl:
doc = fitz.open(fn)
page = doc[1]
trans = fitz.Matrix(zoom / 100.0, zoom / 100.0)
pm = page.get_pixmap(matrix=trans)
s = os.path.basename(fn).split('.')[0].rjust(5,'0')
lurl=f'pic/{s}.jpg'
pm.save(lurl)
fl=glob.glob('./pic/*.jpg')
fl.sort()
a4inpt = (img2pdf.mm_to_pt(210),img2pdf.mm_to_pt(297))
layout_fun = img2pdf.get_layout_fun(a4inpt)
with open("检查.pdf","wb") as f:
f.write(img2pdf.convert(fl,layout_fun=layout_fun))
print('ok!')In [ ]:
import fitz
import os
import glob
from multiprocessing.dummy import Pool
def get_image(fn):
doc = fitz.open(fn)
zoom = 100
page = doc[1]
trans = fitz.Matrix(zoom / 100.0, zoom / 100.0)
pm = page.get_pixmap(matrix=trans)
s = os.path.basename(fn).split('.')[0].rjust(5,'0')
lurl=f'pic1/{s}.jpg'
pm.save(lurl)
print(f'{s}已成功生成!')
fi_path = 'file/2/'
fl = glob.glob(f'{fi_path}*.pdf')
pool = Pool(8)
pool.map(get_image,fl)
pool.close()
pool.join()In [ ]:
import os
import glob
import img2pdf
import fitz
fi_path = 'file/2022-09-30/'
zoom = 100
fl = glob.glob(f'{fi_path}*.pdf')
for fn in fl:
doc = fitz.open(fn)
page = doc[1]
trans = fitz.Matrix(zoom / 100.0, zoom / 100.0)
pm = page.get_pixmap(matrix=trans)
s = os.path.basename(fn).split('.')[0].rjust(5,'0')
lurl=f'pic/{s}.jpg'
pm.save(lurl)
fl=glob.glob('./pic/*.jpg')
fl.sort()
a4inpt = (img2pdf.mm_to_pt(210),img2pdf.mm_to_pt(297))
layout_fun = img2pdf.get_layout_fun(a4inpt)
i = 1
n = 1
list1 = []
for fl1 in fl:
list1.append(fl1)
if len(list1) == 200:
with open(f"检查{str(i)}.pdf","wb") as f:
f.write(img2pdf.convert(list1,layout_fun=layout_fun))
i +=1
list1 = []
if len(list1)>0:
with open(f"检查{str(i)}.pdf","wb") as f:
f.write(img2pdf.convert(list1,layout_fun=layout_fun))
print('ok!')In [ ]:
import os
import glob
import img2pdf
fi_path = 'file/2/'
fl=glob.glob('./pic/*.jpg')
fl.sort()
a4inpt = (img2pdf.mm_to_pt(210),img2pdf.mm_to_pt(297))
layout_fun = img2pdf.get_layout_fun(a4inpt)
i = 1
n = 1
list1 = []
for fl1 in fl:
list1.append(fl1)
if len(list1) == 200:
with open(f"检查{str(i)}.pdf","wb") as f:
f.write(img2pdf.convert(list1,layout_fun=layout_fun))
i +=1
list1 = []
if len(list1)>0:
with open(f"检查{str(i)}.pdf","wb") as f:
f.write(img2pdf.convert(list1,layout_fun=layout_fun))
print('ok!')In [ ]:
import os, sys
from PyPDF2 import PdfFileWriter, PdfFileReader, PdfFileMerger
import glob
fi_path = 'file/2/'
fl = glob.glob(f'{fi_path}*.pdf')
pdfWriter = PdfFileWriter()
i = 0
doc2 = fitz.open()
for fn in fl:
pdf_reader = PdfFileReader(fn)
pageObj = pdf_reader.getPage(1)
pdfWriter.addPage(pageObj)
with open('split_114.pdf', 'wb') as pdfOutputFile:
pdfWriter.write(pdfOutputFile)
In [5]:
import pdfplumber
import json
import re
file = "file/01730823-邵希强.pdf"
pdf = pdfplumber.open(file)
list1 = []
dict1 = {}
n = 1
for page in pdf.pages:
# print(page.extract_text())
for pdf_table in page.extract_tables():
list2 = []
table = []
cells = []
for row in pdf_table:
if not any(row):
# 如果一行全为空,则视为一条记录结束
if any(cells):
table.append(cells)
cells = []
elif all(row):
# 如果一行全不为空,则本条为新行,上一条结束
if any(cells):
table.append(cells)
cells = []
table.append(row)
else:
if len(cells) == 0:
cells = row
else:
for i in range(len(row)):
if row[i] is not None and not cells[i]:
cells[i] = row[i]
elif row[i] is not None and cells[i]:
table.append(cells)
cells = row
#cells[i] = row[i]
break
elif row[i] is None and cells[i]:
continue
elif row[i] is None and not cells[i]:
#cells[i] = row[i]
continue
# cells[i] = row[i] if cells[i] is None else cells[i] + row[i]
list2 = []
for row in table:
data =[re.sub('\s+', '', cell) if cell is not None else None for cell in row]
print(data)
#print(json.dumps(data_list, indent=2, ensure_ascii=False))
#with open('Test1.json','a',encoding="utf-8") as file: # json文件的存放位置
# json.dump(data_list, file, ensure_ascii=False)
list2.append(data)
if len(list2) > 0:
dict1[n] = list2
#print(dict1)
#list1.append(list2)
n +=1
with open('Test1.json','w',encoding="utf-8") as file:
json.dump(dict1, file, ensure_ascii=False)
pdf.close()<>:51: SyntaxWarning: invalid escape sequence '\s'
<>:51: SyntaxWarning: invalid escape sequence '\s'
/tmp/ipykernel_1730998/3799497539.py:51: SyntaxWarning: invalid escape sequence '\s'
data =[re.sub('\s+', '', cell) if cell is not None else None for cell in row]
['姓名', '邵希强'] ['性别', '男'] ['测试标准', '国民体质测定标准'] ['身高体重指数', '26.02', '60分'] ['握力', '33.5千克', '50分'] ['肺活量', '3050毫升', '70分'] ['选择反应时', '0.572秒', '75分'] ['指标', '您的结果', '亚洲男性平均值'] ['臀围', '100', '88.82'] ['身高腰围指数', '53.19', '42.79'] ['建议类别', '建议项'] ['增加摄入', '豆类、水果、蔬菜、坚果类、蛋类、菌类、乳制品、肉类'] ['生活习惯', '提高睡眠质量、保持心情舒畅、换季时避开感染源、减少用眼']
In [13]:
import pdfplumber
name = 'file/01730823-邵希强.pdf'
pdf = pdfplumber.open(name)
tables =pdf.pages[1].extract_tables()
df1 = tables
for item in df1:
print(item)[['姓名', '邵希强']] [['性别', '男']] [['测试标准', '国民体质测定标准']] [['身高体重指数', '26.02', '60分']] [['握力', '33.5 千克', '50分']] [['肺活量', '3050 毫升', '70分']] [['选择反应时', '0.572 秒', '75分']]
In [30]:
import pdfplumber
name = 'file/01730823-邵希强.pdf'
pdf = pdfplumber.open(name)
text = pdf.pages[1].extract_text()#######页码从0开始计数
#print(text)
list1 = text.split('\n')
print(list1)
for item in list1:
if '测试标准 国民体质测定标准' in item:
list_min = list1.index(item)
if '请注意:以上测试项目' in item:
list_max = list1.index(item)
print(list_min,list_max)
for i in range(list_min+1,list_max):
print(list1[i])
['北体乾元健康管理中心', '姓名 邵希强', '中石化(天津)石油化工 编号 01730823', '有限公司 性别 男', '年龄 53', '国民体质检测结果与健康处方', '肺活量', '握力 身高体重指数', '坐位体前屈 选择反应时', '纵跳 闭眼单脚站立', '俯卧撑', '测试标准 国民体质测定标准', '闭眼单脚站立 2.8 秒 10分', '身高体重指数 26.02 60分', '坐位体前屈 13.5 厘米 90分', '握力 33.5 千克 50分', '纵跳 18.0 厘米 30分', '肺活量 3050 毫升 70分', '俯卧撑 5 次 50分', '选择反应时 0.572 秒 75分', '腰臀比 0.90 正常', '请注意:以上测试项目及格线为3分。列表中红色项目需重点关注,建议在专家指导下进行科学锻炼,', '减少潜在的运动风险。', '感谢您完成测试,以《国民体质测定标准》综合评级,您的总分为58分,等级为四级(不合格)。', '(其中项目纵跳因超出年龄范围得分仅供参考,不计入总分)', '北体乾元体质监测报告'] 11 21 闭眼单脚站立 2.8 秒 10分 身高体重指数 26.02 60分 坐位体前屈 13.5 厘米 90分 握力 33.5 千克 50分 纵跳 18.0 厘米 30分 肺活量 3050 毫升 70分 俯卧撑 5 次 50分 选择反应时 0.572 秒 75分 腰臀比 0.90 正常
In [ ]: