![LibreOffice,odt文件批量搜索和替换](https://linux22.com/image/1465214/LibreOffice%EF%BC%8Codt%E6%96%87%E4%BB%B6%E6%89%B9%E9%87%8F%E6%90%9C%E7%B4%A2%E5%92%8C%E6%9B%BF%E6%8D%A2.png)
我正在使用适用于 Windows 7 的 LibreOffice Writer(版本:5.0.1.2),我有多个文件需要替换其中的某些文本。我找不到批量搜索和替换多个文件的方法。
答案1
我创建了一个可能有帮助的 Python 脚本。在此示例中,我提示在各个文件中填充详细信息,然后创建一个项目目录并写入更新了变量的新文件。
“date”、“project_description”和“client”是我的变量。
我确信这可以适合您的解决方案。如果您有更具体的用例,我可以更新我的代码提交。
from docxtpl import DocxTemplate
from datetime import date
today = date.today()
destination_folder = raw_input("Supply a name for the folder ? ")
if not destination_folder:
destination_folder = 'SBD_templates (copy)'
document_date = raw_input("What is the document date (default : " + today.strftime(" %d %B %Y") + ") ? ")
if not document_date:
document_date = today.strftime(" %d %B %Y")
document_project = raw_input("What is the project name ? ")
if not document_project:
document_project = 'Project Not supplied'
document_client = raw_input("What is the client name ? ")
if not document_client:
document_client = 'Client Not supplied'
context = { 'date' : document_date , 'project_description' : document_project, 'client' : document_client }
# Copy the template directory
from distutils.dir_util import copy_tree
# copy subdirectory example
fromDirectory = "./SBD_templates"
toDirectory = "./" + destination_folder
copy_tree(fromDirectory, toDirectory)
doc = DocxTemplate(destination_folder + "/SBD_4.docx")
doc.render(context)
doc.save(destination_folder + "/05_SBD_4.docx")
doc = DocxTemplate(destination_folder + "/SBD_6_1.docx")
doc.render(context)
doc.save(destination_folder + "/06_SBD_6_1.docx")
doc = DocxTemplate(destination_folder + "/SBD8.docx")
doc.render(context)
doc.save(destination_folder + "/07_SBD8.docx")
doc = DocxTemplate(destination_folder + "/SBD9.docx")
doc.render(context)
doc.save(destination_folder + "/08_SBD9.docx")