word批量替换指定值

import tkinter as tk
import xlrd
from docx import Document
from docx.enum.table import WD_TABLE_ALIGNMENT
from docx.shared import Pt
from os import system
from docx.oxml.ns import qn  # 中文格式
from tkinter import filedialog, messagebox
'''打开选择文件夹对话框'''
window = tk.Tk()
window.title("规划坐标合并1.0  cby: 放放风")
window.geometry('790x465+500+200')  # 290 160为窗口大小,+1000 +10 定义窗口弹出时的默认展示位置
window['background'] = 'Pink'

xlspath = filedialog.askopenfilenames(filetypes=[("文本", "xls")], title="请选择需要合并的规划文件")
Folderpath = filedialog.askopenfilenames(filetypes=[("文本", "docx")], title="请选择需要合并的规划文件")
openname = []
for ui in xlspath:
    wb = xlrd.open_workbook(ui)
    ws = wb.sheet_by_name("瓶窑镇乱占耕地建房整体情况分析表")
    for n in range(1,ws.nrows):#循环表格每一行
        name = str(ws.cell_value(n, 0))#编号
        xuhao = str(ws.cell_value(n,0))[1:]
        weiyima = ws.cell_value(n,1)
        ctype = type(ws.cell_value(n,1))
        if isinstance(weiyima,float):
            weiyima = str(int(weiyima))
            print(type(weiyima))
        cun = str( ws.cell_value( n, 2 ) )
        bnryy = str(ws.cell_value(n,7))
        val = str(ws.cell_value(n, 3))#地址
        oo = len(val)
        for path in Folderpath:#循环选择的docx路径
            path_names = path.split("/")

            path_name = path_names[-1][:-5]
            aname = name[1:]#  选择文件A 删除"A"+ ,B 添加"A"+

            if aname == path_name:#如果编号等于文件名!

                doc = Document(path)#打开该路径docx
                tabs = doc.tables#表
                table = tabs[0]#表0
                table.cell(1, 3).text = ""#表指定位置写入空
                run = table.cell(1, 3).paragraphs[0].add_run(val)#指定位置添加val
                # 重写表格
                table.cell( 1, 0 ).text = "" #表格1行0列
                run2 = table.cell(1, 0).paragraphs[0].add_run(xuhao)
                run2.font.name = "仿宋_GB2312"
                run2._element.rPr.rFonts.set( qn( 'w:eastAsia' ), u'仿宋_GB2312' )
                run2.font.size = Pt( 10 )  # 设置字体和大小
                table.cell( 1, 0 ).paragraphs[0].paragraph_format.alignment = WD_TABLE_ALIGNMENT.CENTER

                table.cell( 1, 1 ).text = "" #表格1行1列
                run3 = table.cell(1, 1).paragraphs[0].add_run(weiyima)
                run3.font.name = "仿宋_GB2312"
                run3._element.rPr.rFonts.set( qn( 'w:eastAsia' ), u'仿宋_GB2312' )
                run3.font.size = Pt( 10 )  # 设置字体和大小
                table.cell( 1, 1 ).paragraphs[0].paragraph_format.alignment = WD_TABLE_ALIGNMENT.CENTER

                table.cell( 1, 2 ).text = "" #表格1行2列
                run4 = table.cell(1, 2).paragraphs[0].add_run(cun)
                run4.font.name = "仿宋_GB2312"
                run4._element.rPr.rFonts.set( qn( 'w:eastAsia' ), u'仿宋_GB2312' )
                run4.font.size = Pt( 10 )  # 设置字体和大小
                table.cell( 1, 2 ).paragraphs[0].paragraph_format.alignment = WD_TABLE_ALIGNMENT.CENTER

                table.cell( 1, 8 ).text = "" #表格1行2列
                run5 = table.cell(1, 8).paragraphs[0].add_run(bnryy)
                run5.font.name = "仿宋_GB2312"
                run5._element.rPr.rFonts.set( qn( 'w:eastAsia' ), u'仿宋_GB2312' )
                run5.font.size = Pt( 10 )  # 设置字体和大小
                table.cell( 1, 8 ).paragraphs[0].paragraph_format.alignment = WD_TABLE_ALIGNMENT.CENTER


                if oo <20:
                    run.font.name = "仿宋_GB2312"
                    run._element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
                    run.font.size = Pt(9)#设置字体和大小
                elif oo < 24:
                    run.font.name = "仿宋_GB2312"
                    run._element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
                    run.font.size = Pt(7)#设置字体和大小
                elif oo < 28:
                    run.font.name = "仿宋_GB2312"
                    run._element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
                    run.font.size = Pt(6.5)#设置字体和大小
                else:
                    # print( name, oo,path)
                    run.font.name = "仿宋_GB2312"
                    run._element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋_GB2312')
                    run.font.size = Pt(5.5)#设置字体和大小
                    openname.append(path)
                    print( aname, path_name )
                table.cell(1, 3).paragraphs[0].paragraph_format.alignment = WD_TABLE_ALIGNMENT.CENTER#居中
                doc.save(path)#保存文档


for path23 in openname:
    system( "start %s" % path23 )

tk.messagebox.showinfo('结果', "成功t")

window.mainloop()


你可能感兴趣的:(python)