2

I am using LibreOffice Writer (Version: 5.0.1.2) for Windows 7, and I have multiple files that I need to replace some text in them.. I can't find a way to search and replace in many files in bulk.

Mike
  • 397

1 Answers1

1

I created a python script that might help. In this example, I prompt for the details to be populated across various files, then create a project directory and write the new file with the variables updated.

'date', 'project_description' and 'client' are my variables.

I am sure this can be adapted for your solution. if you have a more specific use case, I can update my code submission.

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")
crafter
  • 111