首页 > 编程知识 正文

python批量替换脚本(自动替换文件脚本)

时间:2023-12-20 16:55:06 阅读:318510 作者:BVZB

本文目录一览:

请PYTHON大神指点?

第二个 with 要等到检测到需要修改之后再打开文件,因为 w 模式会自动清空文本内容。

word文字替换批处理之python

 媳妇有无数word文档要替换,百度后发现没有现成的方法。

google后没有太合适的。抄抄写写弄个python脚本换目录下所有word内容,共勉之。

import os

from docx import Document

# 放了一些docx 文件

files_dict ={

"/home/test/a/医疗器械临床试验第一版-设计/": "/home/test/a/医疗器械临床试验第一版-设计/",

  "/home/test/a/医疗器械临床试验第一版-管理制度/": "/home/test/a/医疗器械临床试验第一版-管理制度/",

"/home/test/a/医疗器械临床试验第一版-SOP/": "/home/test/a/医疗器械临床试验第一版-SOP/",

"/home/test/a/目录/": "/home/test/a/目录/"

}

replace_dict = {

    "XXGNK":"XZDXGWK",

    "心血管专业": "心脏大血管外科",

    "心血管":"心脏大血管外科",

}

def check_and_change(document, replace_dict):

    """

    遍历word中的所有 paragraphs,在每一段中发现含有key 的内容,就替换为 value 。

    (key 和 value 都是replace_dict中的键值对。)

    """

    for para in document.paragraphs:

        for i in range(len(para.runs)):

            for key, value in replace_dict.items():

                if key in para.runs[i].text:

                    print(key+"--"+value)

                    para.runs[i].text = para.runs[i].text.replace(key, value)

    for table in document.tables:

        for row in table.rows:

            for cell in row.cells:

                for para in cell.paragraphs:

                    for i in range(len(para.runs)):

                        for key, value in replace_dict.items():

                            if key in para.runs[i].text:

                                print(key+"--"+value)

                                para.runs[i].text = para.runs[i].text.replace(key, value)

    return document

def main():

    for old_file_path, new_file_path in files_dict.items():

        for name in os.listdir(old_file_path):

            print(name)

            old_file = old_file_path + name

            new_file = new_file_path + name

            if old_file.split(".")[1] == 'docx':

                document = Document(old_file)

                document = check_and_change(document, replace_dict)

                document.save(new_file)

            print("^"*30)

if __name__ == '__main__':

    main()

求助,怎么运用Python脚本批量替换mxd文件中的文本?

import arcpy, string, os

#Read input parameters from script tool

Path = arcpy.GetParameterAsText(0)

oldText = arcpy.GetParameterAsText(1)

newText = arcpy.GetParameterAsText(2)

case = arcpy.GetParameter(3)

exact = arcpy.GetParameter(4)

outputMXD = arcpy.GetParameterAsText(5)

try:

    #Referent the map document

    mxd = arcpy.mapping.MapDocument(mxdPath)        

    #Find all page layout text elements

    for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT"):     

        if exact:

            if case:

                if oldText == elm.text:

                    elmText = elm.text.replace(oldText, newText)

                    elm.text = elmText

            else:

                if oldText.upper() == elm.text.upper():

                    elmText = elm.text.upper().replace(oldText, newText)

                    elm.text = elmText   

        else:

            if case:

                if oldText in elm.text:

                    elmText = elm.text.replace(oldText, newText)

                    elm.text = elmText

            else:

                if oldText.upper() in elm.text.upper():

                    elmText = elm.text.upper().replace(oldText, newText)

                    elm.text = elmText                  

    mxd.saveACopy(outputMXD)

    del mxd

except Exception, e:

    import traceback

    map(arcpy.AddError, traceback.format_exc().split("n"))

    arcpy.AddError(str(e))

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。