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')
标签:xlsx,old,string,doc,cell,file,path,new,xls
From: https://www.cnblogs.com/alfredsun/p/18283826