Re: [Tutor] Attempt to overwrite excel cell Python
Hi Walter! I already looked at the code you suggested me to accomplish my goal, I made some changes on it to match mine, your code is very neat and nice. Here are some lines I changed: - First I did the file search file_list = [] folders = None for root, folders, files in os.walk( C:\\ ): file_list.extend(os.path.join(root,fi) for fi in files if fi.endswith(.shp)) - Then I create the excel, and asign the writing to the cells: wksht = wrkbk.add_sheet('shp') wksht.row(0).write(0,'archivo') wksht.row(0).write(1,'x_min') wksht.row(0).write(2,'y_min') wksht.row(0).write(3,'x_max') wksht.row(0).write(4,'y_max') wksht.row(0).write(5,'geometria') wksht.row(0).write(6,'num_elem') wksht.row(0).write(7,'prj') wksht.row(0).write(8,'estrutura bd') wksht.row(0).write(9,'extent') wksht.row(0).write(10,'fecha_modificacion') wksht.row(0).write(11,'maquina_host') for row, filepath in enumerate(file_list, start=1): wksht.row(row).write(0, filepath) wrkbk.save('shp.xls') SEARCH_PATH = os.getcwd() TARGET_FILE = os.path.realpath('shp.xls') print Searching in, SEARCH_PATH, and writing to, TARGET_FILE print Finding files... print Writing Excel file... print Done. 2010/10/13 Susana Iraiis Delgado Rodriguez susana.delgad...@utzmg.edu.mx Hi Walter! I already looked at the code you suggested me to accomplish my goal, I made some changes on it to match mine, your code is very neat and nice. Here are some lines I changed: - First I did the file search file_list = [] folders = None for root, folders, files in os.walk( C:\\ ): file_list.extend(os.path.join(root,fi) for fi in files if fi.endswith(.shp)) - Then I create the excel, and asign the writing to the cells: wksht = wrkbk.add_sheet('shp') wksht.row(0).write(0,'archivo') wksht.row(0).write(1,'x_min') wksht.row(0).write(2,'y_min') wksht.row(0).write(3,'x_max') wksht.row(0).write(4,'y_max') wksht.row(0).write(5,'geometria') wksht.row(0).write(6,'num_elem') wksht.row(0).write(7,'prj') wksht.row(0).write(8,'estrutura bd') wksht.row(0).write(9,'extent') wksht.row(0).write(10,'fecha_modificacion') wksht.row(0).write(11,'maquina_host') for row, filepath in enumerate(file_list, start=1): wksht.row(row).write(0, filepath) wrkbk.save('shp.xls') SEARCH_PATH = os.getcwd() TARGET_FILE = os.path.realpath('shp.xls') print Searching in, SEARCH_PATH, and writing to, TARGET_FILE print Finding files... print Writing Excel file... print Done. 2010/10/12 Walter Prins wpr...@gmail.com Hi just a quick note -- I included my own recursive call in the code I posted last night which is obviously superflous as os.walk() already recurses by itself. So if you looked at it already you may want to look at it again. Walter ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Attempt to overwrite excel cell Python
Hello members: I'm using xlwt from python to work with excel, I'm developing some sort of system crawler to look for all the files ending with .shp, I took a look to csv, but I didn't understand it. Now, I have to create a .xls where people can save information from shp files, but first I have to create the excel and add a row for column names: import os from xlwt import Workbook wb = Workbook() ws = wb.add_sheet('shp') ws.row(0).write(0,'archivo') ws.row(0).write(1,'x_min') ws.row(0).write(2,'y_min') ws.row(0).write(3,'x_max') ws.row(0).write(4,'y_max') ws.row(0).write(5,'geometria') ws.row(0).write(6,'num_elem') ws.row(0).write(7,'prj') ws.row(0).write(8,'estrutura bd') ws.row(0).write(9,'extent') ws.row(0).write(10,'fecha_modificacion') ws.row(0).write(11,'maquina_host') Then I have to do the crawler part to have the directory I want: allfiles = [] #store all files found for root,dir,files in os.walk(C:\\): filelist = [ os.path.join(root,fi) for fi in files if fi.endswith(.shp) ] for f in filelist: allfiles.append(f) Now I want to get the name of the file and write it uder the column Archivo, the variable i has the name of each file: for i in allfiles: print i ws.row(1).write(0,i) wb.save('shp.xls') But when I run it, it throws me the error: *Exception*: *Attempt to overwrite cell*: sheetname='shp' rowx=1 colx=0, don't know what is wrong ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Attempt to overwrite excel cell Python
On Tue, 12 Oct 2010 07:08:15 am Susana Iraiis Delgado Rodriguez wrote: But when I run it, it throws me the error: *Exception*: *Attempt to overwrite cell*: sheetname='shp' rowx=1 colx=0, don't know what is wrong Please don't summarise the error. Copy and paste the exact error message, including the full traceback. I suspect that you might need to read the documentation of the xlwt project and see what it says. Perhaps the cell you are trying to overwrite is locked? -- Steven D'Aprano ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Attempt to overwrite excel cell Python
Hi Susana, On 11 October 2010 21:08, Susana Iraiis Delgado Rodriguez susana.delgad...@utzmg.edu.mx wrote: *Exception*: *Attempt to overwrite cell*: sheetname='shp' rowx=1 colx=0, don't know what is wrong This is a default of xlwt behaviour for writing Excel files. It assumes it would generally be an unintended mistake to write a cell then overwrite it later with something else hence it by default raises an exception when this happens. Anyway, you can override this behaviour and enable cell overwriting by adding the parameter cell_overwrite_ok=True when you create a worksheet. However I think you probably are just accidentally (and erroneously) overwriting the same row accidentally, so the error is probably meaningful in your case. Wahtever the case, I've taken the liberty to knock what you posted into shape a bi -- The following script now works (well it finds files and doesn't generate the error you reported.) I've also tidied up the structure and formatting as you'll see: http://pastebin.com/Kc9N3DAC Hope that helps, Walter ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor