第一步 获取图片
import shutil
import os
import easygui as g
import zipfile
'''
自动从服务器的文件夹中的提取出质量控制报告中的图片
'''
path = g.diropenbox()
g = os.walk(path)
for path,dir_list,file_list in g:
for file_name in file_list:
if "质控报告.docx" in os.path.join(path, file_name):
picpath = os.path.join(path, file_name)
print(picpath)
shutil.copy(picpath, picpath.replace('质控报告.docx', '质控报告_副本.docx'))
zip_path = picpath.replace('质控报告_副本.docx', '质控报告.zip')
os.rename( picpath, zip_path)
f = zipfile.ZipFile( zip_path, 'r')
tmp_path = picpath.replace( '\\质控报告.docx', '')
for file in f.namelist():
f.extract( file, tmp_path )
f.close()
pic = os.listdir( os.path.join( tmp_path, 'word/media'))
for i in pic:
new_name = path.replace( '\\', '_')
new_name = new_name.replace( ':', '') + '_' + i
shutil.copy( os.path.join( tmp_path + '/word/media', i), os.path.join( r'C:\Users\Public\Documents\Word', new_name))
shutil.rmtree( tmp_path + '\\_rels')
shutil.rmtree( tmp_path + '\\customXml')
shutil.rmtree( tmp_path + '\\docProps')
shutil.rmtree( tmp_path + '\\word')
os.remove( tmp_path + '\\[Content_Types].xml')
第二步 编辑word报告模板样式
from docx import Document
from docx.shared import Inches
from docx.oxml.ns import qn
from docx.shared import Pt
from docx.enum.text import WD_ALIGN_PARAGRAPH
def doc(pic, well, num, layer, deep):
doc = Document()
p = doc.add_paragraph()
run = p.add_run( '全岩心CT扫描检测质量控制报告' )
run.font.size = Pt( 18 )
p.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER
run.font.name = 'Times New Roman'
run._element.rPr.rFonts.set( qn( 'w:eastAsia'), '黑体')
p1 = doc.add_paragraph()
run1 = p1.add_run('一、基础资料')
run1.font.size = Pt( 10.5 )
run1.font.name = 'Time New Roman'
run1._element.rPr.rFonts.set( qn( 'w:eastAsia'), '黑体')
p1.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.LEFT
table1 = doc.add_table( rows=3, cols=4 )
table1.style = doc.styles['Table Grid']
table1.name = 'Time New Roman'
table1.cell( 0, 0).text = "井号"
table1.cell( 0, 1).text = "%s" % well
table1.cell( 0, 2).text = "编号"
table1.cell( 0, 3).text = "%s" % num
table1.cell( 1, 0).text = "层位"
table1.cell( 1, 1).text = "%s" % layer
table1.cell( 1, 2).text = "井段,m"
table1.cell( 1, 3).text = "%s" % deep
table1.cell( 2, 0).text = "检测温度,℃"
table1.cell( 2, 1).text = "22.0"
table1.cell( 2, 2).text = "检测日期"
table1.cell( 2, 3).text = "2021-6-19"
table1.cell( 0, 0).paragraphs[0].alignment = WD_ALIGN_PARAGRAPH.CENTER
table1.cell( 0, 1).paragraphs[0].alignment = WD_ALIGN_PARAGRAPH.CENTER
table1.cell( 0, 2).paragraphs[0].alignment = WD_ALIGN_PARAGRAPH.CENTER
table1.cell( 0, 3).paragraphs[0].alignment = WD_ALIGN_PARAGRAPH.CENTER
table1.cell( 1, 0).paragraphs[0].alignment = WD_ALIGN_PARAGRAPH.CENTER
table1.cell( 1, 1).paragraphs[0].alignment = WD_ALIGN_PARAGRAPH.CENTER
table1.cell( 1, 2).paragraphs[0].alignment = WD_ALIGN_PARAGRAPH.CENTER
table1.cell( 1, 3).paragraphs[0].alignment = WD_ALIGN_PARAGRAPH.CENTER
table1.cell( 2, 0).paragraphs[0].alignment = WD_ALIGN_PARAGRAPH.CENTER
table1.cell( 2, 1).paragraphs[0].alignment = WD_ALIGN_PARAGRAPH.CENTER
table1.cell( 2, 2).paragraphs[0].alignment = WD_ALIGN_PARAGRAPH.CENTER
table1.cell( 2, 3).paragraphs[0].alignment = WD_ALIGN_PARAGRAPH.CENTER
p2 = doc.add_paragraph()
run2 = p2.add_run( '二、检测参数')
run2.font.size = Pt( 10.5 )
run2.font.name = 'Time New Roman'
run2._element.rPr.rFonts.set( qn( 'w:eastAsia'), '黑体')
p2.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.LEFT
table2 = doc.add_table(rows=2, cols=5)
table2.style = doc.styles['Table Grid']
table2.cell( 0, 0).text = "高能扫描电压\nkV"
table2.cell( 0, 1).text = "低能扫描电压\nkV"
table2.cell( 0, 2).text = "扫描电流\nmA"
table2.cell( 0, 3).text = "像素尺寸\nmm"
table2.cell( 0, 4).text = "切片厚度\nmm"
table2.cell( 1, 0).text = "140"
table2.cell( 1, 1).text = "100"
table2.cell( 1, 2).text = "100"
table2.cell( 1, 3).text = "0.49"
table2.cell( 1, 4).text = "0.625"
table2.cell( 0, 0).paragraphs[0].alignment = WD_ALIGN_PARAGRAPH.CENTER
table2.cell( 0, 1).paragraphs[0].alignment = WD_ALIGN_PARAGRAPH.CENTER
table2.cell( 0, 2).paragraphs[0].alignment = WD_ALIGN_PARAGRAPH.CENTER
table2.cell( 0, 3).paragraphs[0].alignment = WD_ALIGN_PARAGRAPH.CENTER
table2.cell( 0, 4).paragraphs[0].alignment = WD_ALIGN_PARAGRAPH.CENTER
table2.cell( 1, 0).paragraphs[0].alignment = WD_ALIGN_PARAGRAPH.CENTER
table2.cell( 1, 1).paragraphs[0].alignment = WD_ALIGN_PARAGRAPH.CENTER
table2.cell( 1, 2).paragraphs[0].alignment = WD_ALIGN_PARAGRAPH.CENTER
table2.cell( 1, 3).paragraphs[0].alignment = WD_ALIGN_PARAGRAPH.CENTER
table2.cell( 1, 4).paragraphs[0].alignment = WD_ALIGN_PARAGRAPH.CENTER
p3 = doc.add_paragraph()
run3 = p3.add_run( '三、检测图片' )
run3.font.size = Pt( 10.5 )
run3.font.name = 'Time New Roman'
run3._element.rPr.rFonts.set( qn( 'w:eastAsia'), '黑体')
p3.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.LEFT
doc.add_picture(pic, width=Inches( 6 ))
last_paragraph = doc.paragraphs[-1]
last_paragraph.alignment = WD_ALIGN_PARAGRAPH.CENTER
p6 = doc.add_paragraph()
run6 = p6.add_run( '岩心毫米CT扫描检测图像')
run6.font.size = Pt( 10 )
p_format = p6.paragraph_format
p_format.alignment = WD_ALIGN_PARAGRAPH.CENTER
p4 = doc.add_paragraph()
run4 = p4.add_run( '四、检测结果')
run4.font.size = Pt( 10.5 )
run4.font.name = 'Time New Roman'
run4._element.rPr.rFonts.set( qn( 'w:eastAsia'), '黑体')
p4.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.LEFT
p5 = doc.add_paragraph()
run5 = p5.add_run( '全部扫描图片无伪影,质量合格。')
run5.font.size = Pt( 10.5 )
p_format = p5.paragraph_format
p_format.first_line_indent = Inches( 0.2 )
doc.add_page_break()
doc.save( pic + '.docx')
第三步 批量生成报告
from gereport import doc
import os
import csv
import easygui as g
import shutil
'''
自动生成报告,提前准备好一个为information.csv的配置文件,该文件记录报告中表格内容的相关信息,启动小程序时选择打开该文件
'''
path = g.fileopenbox()
print(path)
with open(path) as f:
reader = csv.reader(f)
well = [row[1] for row in reader]
print(well)
with open(path ) as f:
reader2 = csv.reader( f )
number = [row[2] for row in reader2]
print( number )
with open(path) as f:
reader3 = csv.reader( f )
layer = [row[3] for row in reader3]
print( layer )
with open(path ) as f:
reader4 = csv.reader( f )
deep = [row[4] for row in reader4]
print( deep )
print(type(deep))
print(deep[0])
rootdir = r'C:\Users\Public\Documents\Word'
savedir = r'C:\Users\Public\Documents\res'
list = os.listdir( rootdir)
print(list)
k = 0
while k < len(well):
for i in list:
res = rootdir + '\\' + i
well1 = well[k]
number1 = number[k]
layer1 = layer[k]
deep1 = deep[k]
doc(res, well1, number1, layer1, deep1)
k += 1
list2 = os.listdir(rootdir)
print(list2)
for j in list2:
print( j )
ja = j
if ".docx" in ja:
print(ja)
comw = rootdir + '\\' + ja
savew = savedir + '\\' + ja
shutil.copy(comw, savew)
第四步 快速将多个文件合并为一个
import os
from os.path import abspath
from win32com import client
rootdir = r'C:\Users\Public\Documents\Word'
savedir = r'C:\Users\Public\Documents\res'
list3 = os.listdir(savedir)
print(list3)
list5 = []
for i in list3:
list4 = r'C:\Users\Public\Documents\res' + '\\' + i
list5.append(list4)
print(list5)
def main(files, final_docx):
word = client.gencache.EnsureDispatch("Word.Application")
word.Visible = True
new_document = word.Documents.Add()
for fn in files:
fn = abspath(fn)
temp_document = word.Documents.Open(fn)
word.Selection.WholeStory()
word.Selection.Copy()
temp_document.Close()
new_document.Range()
word.Selection.Delete()
word.Selection.Paste()
new_document.SaveAs(final_docx)
new_document.Close()
word.Quit()
main(list5, r'C:\Users\Public\Documents\res\result.docx')