3

I have multiple .QGS files and I would like to export them to .QGZ.

Right now I'm opening the .QGS file and save as .QGZ one by one, but I have over 180 .QGS files so I'm looking for a way to automate it.

I am using QGIS 3.30 and Windows 10 if that matters, but I could set up a virtual machine with Linux if that helps.

0

3 Answers 3

4

I was able to solve it with the following Powershell commands:

##copying .qgs to another directory
cp *.qgs ..\dir\

##changing to that dir
cd ..\dir\

##zipping .qgs
Get-ChildItem -Filter *.qgs | ForEach-Object { Compress-Archive -Path $_.FullName -CompressionLevel Optimal -DestinationPath ($_.FullName + ".zip") }

##Changing the extension from .zip to .qgz
dir | rename-item -NewName {$_.name -replace ".qgs.zip",".qgz"}

##Deleting everything that's not -qgz
Get-ChildItem | Where-Object { $_.Extension -ne ".qgz" } | Remove-Item
3

QGIS .qgz files are merely zip containers which have the .qgs file embedded, together with potentially some sidecar files.

I have just tried, and on a simple project just zipping the .qgs file alone into a .qgz file generates a usable .qgz project. In recent versions of QGIS, it is missing the project styles database (ending in .db, see https://github.com/qgis/QGIS/commit/c6beb62216fcfbfe3ca64283c09117da53800a98 ) but no errors or warnings appear to be generated, so if you aren't using multiple project style databases functionality, you should be fine.

Assuming this is the case, just therefore use your favourite zip utility to zip up each .qgs file to a .qgz file, say using the command line.

For instance, if you have 7zip installed, the following CMD line will iterate and zip up all .qgs files in the current directory into .qgs files

for %a in (*.qgs) do "C:\Program Files\7-Zip\7z" a -tzip "%~na.qgz" "%a"

That's assuming the directory is right for 7z.exe. The for loop uses the variable %a to loop through all qgs files, invoking 7zip to make a ZIP (not 7Z) format archive (that's the a -tzip) of the corresponding .qgz file (%~na.qgz means the stripped filename of variable %a with .qgz added). You may need to modify this based on how/where the qgs files are, where 7zip is, and what other zip utility you may prefer.

3

Save the following script as .py file. Change the folder path.

import glob
from qgis.core import QgsApplication, QgsProject

qgs = QgsApplication([], False)
qgs.initQgis()
project = QgsProject.instance()

# folder containing QGS files
folder = "c:/path/to/folder/"

for f in glob.glob(folder + '*.qgs'):
    project.read(f)
    project.write(f[:-3] + 'qgz')

Then

  1. Open "OSGeo Shell"
  2. Navigate to the folder containing the .py file
  3. Run the file.

enter image description here

enter image description here


You can also run this script in QGIS Python Editor:

import glob
project = QgsProject.instance()

folder = "c:/path/to/folder/"

for f in glob.glob(folder + '*.qgs'):
    project.read(f)
    project.write(f[:-3] + 'qgz')
2
  • I'm having trouble getting the script to work. I get the following error: D:\Pablo\pruebas\pruebaQGIS2>python script-prueba.py Traceback (most recent call last): File "D:\Pablo\pruebas\pruebaQGIS2\script-prueba.py", line 2, in <module> from qgis.core import QgsApplication, QgsProject ModuleNotFoundError: No module named 'qgis'
    – pfrud
    Commented Mar 20, 2023 at 12:21
  • @pfrud You can also run the edited script (as in the answer) in QGIS Python Editor. Commented Mar 20, 2023 at 19:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.