用python写一个遍历文件夹及子文件夹中所有doc/docx文件并且替换其中指定汉字的函数
时间: 2024-12-15 17:20:17 浏览: 13
在Python中,你可以使用`os`模块来遍历文件夹及其子文件夹,并结合`pathlib`库来处理文件路径,`openpyxl`或`python-docx`库来操作`.doc`或`.docx`文档。这里是一个基础的函数示例,它会查找并替换指定的汉字:
```python
import os
from typing import List
import docx
def replace_char_in_doc_files(root_folder: str, target_char: str, replacement_char: str) -> List[str]:
replaced_files = []
# 遍历文件夹及其子文件夹
for dirpath, _, filenames in os.walk(root_folder):
for filename in filenames:
if filename.endswith(('.doc', '.docx')):
file_path = os.path.join(dirpath, filename)
try:
# 打开文档
with docx.Document(file_path) as doc:
# 搜索并替换目标字符
for paragraph in doc.paragraphs:
text = paragraph.text
new_text = text.replace(target_char, replacement_char)
# 如果文本有变化,保存新的文档
if text != new_text:
new_file_name = f"{filename}_replaced_{target_char}_{replacement_char}"
new_file_path = os.path.join(dirpath, new_file_name)
doc.save(new_file_path)
replaced_files.append(new_file_path)
except Exception as e:
print(f"Error processing {file_path}: {e}")
return replaced_files
# 使用函数
root_folder = 'your_directory_path' # 替换为你的文件夹路径
target_char = '原字符' # 要替换的目标字符
replacement_char = '新字符' # 替换的新字符
replaced_files = replace_char_in_doc_files(root_folder, target_char, replacement_char)
阅读全文