python修改文件夹名称及文件夹下文件内容替换不改变word格式

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#anthor:kgl time:2024/7/13

import re,os
import sys
from docx import Document
import docx



path = 'E:/pythonplace/测试文件'  # 修改为你要修改文件名的目录路径
# path = 'E:/pythonplace/测试文件'  # 修改为你要修改文件名的目录路径
old_substring = '修改'  # 修改为你要替换的旧文件名字符串
new_substring = '哈哈'  # 修改为你要替换的新文件名符串
file_ext = ['.xls', '.xlsx']
old_words='old' #需要替换的内容
new_words='new' #替换为新的内容

def batch_rename(directory, old_substring, new_substring):
    # 使用os.listdir()读取文件夹中的所有文件和文件夹名
    for filename in os.listdir(directory):
        # 打印所有文件名
        used_name = path +'/'+ filename
        # used_name = os.path.join(path, filename)
        print(used_name)
        if old_substring in filename:
            new_filename = filename.replace(old_substring, new_substring)
            old_path = os.path.join(directory, filename)
            new_path = os.path.join(directory, new_filename)
            os.rename(old_path, new_path)

def alter_content(directory,old_words,new_words):
    for filename in os.listdir(directory):
        # 打印所有文件名
        filename = os.path.join(path, filename)  # 连接当前目录及文件或文件夹名称

        # if (os.path.splitext(used_name)[1]) not in file_ext:
        #     print(used_name)
        if filename.endswith('.docx'):
            
            # 打开Word文档
            # print(filename)
            doc = docx.Document(filename)
        #     # print(doc)
        # # 遍历文档中的所有段落

        #     for para in doc.paragraphs:
        #         for run in para.runs:
        #             # 替换功能
        #             if old_words in run.text:
        #                 run.text = run.text.replace(old_words, new_words)

        #     for para in doc.paragraphs:
        #         para.text = para.text.replace(old_words, new_words)

            for para in doc.paragraphs:
                if old_words in para.text:
                    inline = para.runs
                    for i in inline:
                        if old_words in i.text:
                            print(i.text)
                            text = i.text.replace(old_words, new_words)
                            i.text = text

        #     doc.save(filename)

        # #     遍历文档中的所有表格

            # for table in doc.tables:
            #     # 遍历表格中的所有单元格
            #     for row in table.rows:
            #         for cell in row.cells:
            #             cell.text = cell.text.replace(old_words, new_words)


            # 遍历文档中的所有表格
            for table in doc.tables:
                for row in table.rows:
                    for cell in row.cells:
                        # 遍历表格段落内容,回到上个步骤,将cell当作paragraph处理
                        for paragraph in cell.paragraphs:
                            for run in paragraph.runs:
                                # 替换功能
                                if old_words in cell.text:
                                    # print(cell.text)
                                    cell.text = cell.text.replace(old_words, new_words)
            #     保存修改后的文档
            doc.save(filename)




# 执行批量替换文件夹名称
# batch_rename(path, old_substring, new_substring)
# 替换文件内容
alter_content(path,old_words,new_words)
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值