Skip to content

Instantly share code, notes, and snippets.

Last active October 11, 2022 12:55
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save ThomasG77/409383f7808252794b323e5ab08989da to your computer and use it in GitHub Desktop.
POC pour rechercher remplacer des URLS dans un projet QGIS
import os
import tempfile
import shutil
import argparse
from pathlib import Path
parser = argparse.ArgumentParser()
parser.add_argument("path", type=str,
help="Provide path to a qgs or qgz file")
args = parser.parse_args()
input_file = args.path
filename = input_file.split(os.sep)[-1]
# Could come from a CSV file instead
urls_before_after = [
['', ''],
['htps://', '']
def replaceQgsContent(input_file, hook=lambda content: content):
with open(input_file) as file:
s =
s = hook(s)
with open(input_file, "w") as file:
def replaceText(content):
for urls in urls_before_after:
content = content.replace(urls[0], urls[1])
return content
if filename.split('.')[-1] == 'qgs':
replaceQgsContent(input_file, hook=replaceText)
with tempfile.TemporaryDirectory() as dirpath:
shutil.unpack_archive(input_file, dirpath, format='zip')
# Normally only one
qgs_files = Path(dirpath).glob('*.qgs')
for qgs_file in qgs_files:
replaceQgsContent(qgs_file, hook=replaceText)
shutil.make_archive(input_file, format='zip', root_dir=dirpath)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment