快速生成全直径岩心扫描质控报告

第一步 获取图片

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):
            # print(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')
            # print(f.namelist())

            tmp_path = picpath.replace( '\\质控报告.docx', '')

            # 将图片提取并保存
            for file in f.namelist():
                f.extract( file, tmp_path )
            # 释放该zip文件
            f.close()

            # 将docx文件从zip还原为docx os.rename( zip_path, picpath)
            
            # 得到缓存文件夹中图片列表
            pic = os.listdir( os.path.join( tmp_path, 'word/media'))

            # 将图片复制到最终的文件夹中
            for i in pic:
                # 根据word的路径生成图片的名称
                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扫描检测质量控制报告' )  # 使用add_run添加文字
    run.font.size = Pt( 18 )  # 字体大小设置,和word里面的字号相对应,小一
    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:
        # print( i )
        res = rootdir + '\\' + i
        # print(res)
        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应用程序
    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()
    # 保存最终文件,关闭Word应用程序
    new_document.SaveAs(final_docx)
    new_document.Close()
    word.Quit()

main(list5, r'C:\Users\Public\Documents\res\result.docx')



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值