13 KiB
13 KiB
In [ ]:
import os,sys,shutil
import openpyxl
import math
fi_xls = os.getcwd() + '/file/中国石油化工股份有限公司安庆炼化分公司员工在职人员名单.xlsx'
fi_path = os.getcwd() + '/file/210924'
old = []
dict1 = {}
wb = openpyxl.load_workbook(fi_xls)
sheet = wb.active
depart = []
for n in range(2,sheet.max_row+1):
m_name = sheet.cell(n,1).value.strip()
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 = fi_path = os.getcwd()+'/file/210924/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]}/{dict1[n][0]}.pdf'
if not os.path.exists(n_name):
shutil.copyfile(o_name,n_name)
print(n_name)
#print(dict1)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('5.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")
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 [47]:
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') ok
In [2]:
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') ok
In [69]:
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')
ok
In [ ]: