I can import and open DWG files to geopackage one-by-one with the

Project -> Import/Export -> Import Layers from DWG/DXF

command. This window use the QgsDwgImporter cpp function: https://github.com/qgis/QGIS/tree/master/src/app/dwg

Can I call this function from PyQGIS for batch import?


Currently, this is not possible in pyqgis and is labeled as "Feature Request" in QGIS repository. See issue 33198 for more details.

An alternative approach would be to automate this dialog gui using PyQt5 which is not clean but could serve the purpose. GUI approach will be

  1. Open DWG Import dialog using iface.mainWindow().findChildren(QAction ,"mActionDwgImport")[0].trigger()
  2. Fill the field names programmatically [field names are in qgsdwgimportdialog.cpp] and Click OK Pushbutton programmatically.
  3. Repeat for a list of DWG files.
  • Thanks @programmer-punk When the trigger open the window, my python script stop, and only continue when I close it. How should I fill the fields and push the button?
    – kadarivan
    Jan 6 '20 at 16:25
  • You can create a separate thread which will capture this dialog and fill the form. So one thread will be use to trigger the dialog and other one will be used to fill the form. Both thread can communicate with each other i.e. once the import thread is done with importing DWG file then your main thread can trigger the dialog again. Jan 6 '20 at 17:37
  • If this is complicated for you then you can also follow "automate the boring stuff" appraoch for GUI automation. See this link for more details. Jan 6 '20 at 17:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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