The name of the sole layer in the draft ArcGIS map myfile.mxd is Mächtigkeit which contains the German umlaut ä. I face an error message when using the script mxd2qgs.py (downloaded from https://github.com/fitnr/mxd2qgs) although some workarounds are implemented. The script mxd2qgs_short.py below is, to my point, the most possible reduction of the original script, still producing the error.
It looks like the error occurs from interaction of the two sections Section mapcanvas and Section legend with each other. Turning off Section mapcanvas makes the error message disappear. I don't manage to trace the interaction of the layer name with Section mapcanvas. How should I proceed in mxd2qgs_short.py to handle this problem?
Using doc.toprettyxml() is suggested here.
Script mxd2qgs_short.py:
from xml.dom.minidom import Document
import arcpy
mxd = arcpy.mapping.MapDocument(r"myfile.mxd")
qgs = 'myfile.qgs'
df = arcpy.mapping.ListDataFrames(mxd)[0]
lyrlist = arcpy.mapping.ListLayers(df)
lyr, = lyrlist
doc = Document()
qgis = doc.createElement("qgis")
doc.appendChild(qgis)
legend = doc.createElement("legend")
qgis.appendChild(legend)
unit = doc.createTextNode(df.mapUnits)
units = doc.createElement("units")
units.appendChild(unit)
# Section mapcanvas
mapcanvas = doc.createElement("mapcanvas")
qgis.appendChild(mapcanvas)
mapcanvas.appendChild(units)
# Section legend
legendlayer = doc.createElement("legendlayer")
legendlayer.setAttribute("name",lyr.name.encode('iso-8859-1'))
legend.appendChild(legendlayer)
f = open(qgs,"w")
f.write(doc.toprettyxml())
f.close()
Error message:
Traceback (most recent call last):
File "D:\mxd2qgs_short.py", line 31, in <module>
f.write(doc.toprettyxml())
File "C:\Python27\ArcGIS10.5\lib\xml\dom\minidom.py", line 61, in toprettyxml
return writer.getvalue()
File "C:\Python27\ArcGIS10.5\lib\StringIO.py", line 271, in getvalue
self.buf += ''.join(self.buflist)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position 1: ordinal not in range(128)