The following code allows me to save a text file with .xls extension in Python. When I open it, I receive the following warning:
The file you are trying to open, is in a different format than specified by the file extension. Verify that the file is not corrupted and is from a trusted source before opening the file. Do you want to open the file now?
def saveExcel (layer,name):
# get the path
pathSave=getDirectoryPath()
chdir(pathSave)
filename=name+".xls"
f=open(filename,"w")
# Save fields'names
for field in layer.fields():
f.write(field.name()+ "\t")
f.write("\n")
# Save attributes
for element in layer.getFeatures():
for field in layer.fields():
f.write(str(element.attribute(field.name()))+ "\t")
f.write("\n")
f.close()
-
check this gis.stackexchange.com/questions/212461/… and gis.stackexchange.com/a/174253Fran Raga– Fran Raga2018年09月27日 10:26:16 +00:00Commented Sep 27, 2018 at 10:26
-
1.xls is a binary format. Just because Excel will open CSV text doesn't mean .xls is tab-delimited ASCII. Name your file .txt and you won't get that error.Vince– Vince2018年09月27日 11:45:02 +00:00Commented Sep 27, 2018 at 11:45
1 Answer 1
If you are in QGIS environment you could make a QgsVectorLayer with your data and export that as xlsx:
QgsVectorFileWriter.writeAsVectorFormat(layer, path, "utf-8", layer.crs(), 'xlsx')
-
this command seems to not work any idea why? I have already made the QgsVectorLayersoumik chakraborty– soumik chakraborty2022年01月12日 07:13:56 +00:00Commented Jan 12, 2022 at 7:13