[python]Replace key words of all documents in a directory

2024-9-27 写技术

from docx import Document
 
def replace_text_in_word(file_path, new_path, old_texts, new_text):
    doc = Document(file_path)
    for old_text in old_texts:
        for paragraph in doc.paragraphs:
            if old_text in paragraph.text:
                paragraph.text = paragraph.text.replace(old_text, new_text)
        for table in doc.tables:
            for row in table.rows:
                for cell in row.cells:
                    if old_text in cell.text:
                        cell.text = cell.text.replace(old_text, new_text)
    doc.save(new_path)
 
#
'''
file_path = 'FCRN.docx'  # 
old_text = 'FCRN'
new_text = 'NICHOLASN'
 
replace_text_in_word(file_path, old_text, new_text)
'''

ORG_ROOT = './original_doc'

import os
import sys
import shutil

def traverse_directory(path,new_root, old_texts, new_text):
    for root, dirs, files in os.walk(path):
        for file in files:
            ext = os.path.splitext(file)[1]
            doc_path = os.path.join(root, file)
            new_path = doc_path
            for old_text in old_texts:
                new_path = new_path.replace(old_text, new_text)
            new_path = new_path.replace(ORG_ROOT, new_root)
            new_dir = os.path.dirname(new_path)


            print('\ntraverse_directory:\n', doc_path, '\n', new_path, old_text[0], new_text )

            if not os.path.exists( new_dir ):
                os.makedirs( new_dir )
                print("mkdir:", new_dir)


            
            if ext == '.docx' or ext == '.doc':
                try:
                    replace_text_in_word(doc_path, new_path,  old_texts, new_text)
                    print("replace ok")
                except:
                    if os.path.exists(doc_path):
                        shutil.copy2(doc_path, new_path)
                        print("erros file:", doc_path)
                        print("rename file errors:", new_path)
            else:
                if os.path.exists(doc_path):
                    shutil.copy2(doc_path, new_path)
                    print("rename file others:", new_path)
                
        for dir in dirs:
            doc_path = dir
            new_path = doc_path
            for old_text in old_texts:
                new_path = new_path.replace(old_text, new_text)
            new_path = new_path.replace(ORG_ROOT, new_root)

            if os.path.exists(doc_path):
                shutil.copy2(doc_path, new_path)
                print("new dir2:", new_path)
 

# main

if len(sys.argv) < 2:
    NEW_STR = 'NICHOLAS_ORIGINAL_NICHOLAS'
else:
    NEW_STR = sys.argv[1]


traverse_directory(ORG_ROOT, './new/'+NEW_STR, ['src_str1','src_str2', 'old_str1','old_str2'], NEW_STR)

标签: python

发表评论:

Powered by anycle 湘ICP备15001973号-1