批量替换doc、xls、xlsx文件内容
docx类:
import os from docx import Document from openpyxl import load_workbook def replace_string_in_docx(file_path, old_string, new_string): doc = Document(file_path) for paragraph in doc.paragraphs: if old_string in paragraph.text: paragraph.text = paragraph.text.replace(old_string, new_string) doc.save(file_path) def replace_string_in_xlsx(file_path, old_string, new_string): wb = load_workbook(filename = file_path) for sheet in wb: for row in sheet.iter_rows(): for cell in row: if cell.value and old_string in str(cell.value): cell.value = cell.value.replace(old_string, new_string) wb.save(file_path) def replace_string_in_files(dir_path, old_string, new_string): for root, dirs, files in os.walk(dir_path): for file in files: if file.endswith('.doc'): replace_string_in_docx(os.path.join(root, file), old_string, new_string) elif file.endswith('.xls'): replace_string_in_xlsx(os.path.join(root, file), old_string, new_string) replace_string_in_files('C:\\Users\\dir', 'old', 'new')
doc
import os import win32com.client import datetime def replace_string_in_doc(file_path, old_string, new_string): word = win32com.client.Dispatch("Word.Application") doc = word.Documents.Open(file_path) word.Visible = 0 replacement_count = 0 for i in range(len(doc.Paragraphs)): para = doc.Paragraphs[i] if old_string in para.Range.Text: replacement_count += para.Range.Text.count(old_string) para.Range.Text = para.Range.Text.replace(old_string, new_string) doc.Save() doc.Close() return replacement_count def replace_string_in_xls(file_path, old_string, new_string): excel = win32com.client.Dispatch("Excel.Application") workbook = excel.Workbooks.Open(file_path) excel.Visible = 0 replacement_count = 0 for worksheet in workbook.Worksheets: for row in worksheet.UsedRange.Rows: for cell in row.Cells: if cell.Value: if isinstance(cell.Value, datetime.datetime): cell_value_str = cell.Value.strftime('%m/%d/%Y') if old_string == cell_value_str: replacement_count += 1 cell.Value = datetime.datetime.strptime(new_string, '%m/%d/%Y') else: cell_value_str = str(cell.Value) if old_string in cell_value_str: replacement_count += cell_value_str.count(old_string) cell.Value = cell_value_str.replace(old_string, new_string) workbook.Save() workbook.Close() return replacement_count def replace_string_in_files(dir_path, old_string, new_string): for root, dirs, files in os.walk(dir_path): for file in files: if file.endswith('.doc'): print(f"Processing {file}...") replacement_count = replace_string_in_doc(os.path.join(root, file), old_string, new_string) print(f"Replaced '{old_string}' with '{new_string}' {replacement_count} times in {file}") elif file.endswith('.xls'): print(f"Processing {file}...") replacement_count = replace_string_in_xls(os.path.join(root, file), old_string, new_string) print(f"Replaced '{old_string}' with '{new_string}' {replacement_count} times in {file}") replace_string_in_files('C:\\Users\\dir', 'old', 'new')
分类:
Python
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
2017-07-04 Accesss to dzkj dictz