2121import xlsxwriter ; #version 1.0.2, for writing out xlsx files (ie Excel 2003 onwards)
2222import xlwt ; #version 1.3.0, for writing out xls files (ie Excel 97 and earlier)
2323
24+ ## helper function for making sure a supplied sheet name for Excel worksheet is unique - called recursively up to limit of 3 attempts
25+ def check_sheet_name_unique (test_sheet_name ,call_count = 0 ) :
26+ print ("entering sheet name checker for" ,call_count ,"th time. Current sheet name" ,test_sheet_name )
27+ if call_count < 3 :
28+ print (str (outpath ),str (excelWriterSheets ))
29+ if str (outpath ) in excelWriterSheets :
30+ if test_sheet_name in excelWriterSheets [str (outpath )] :
31+ print (excelWriterSheets [str (outpath )])
32+ if len (test_sheet_name ) < (30 - len (str (len (excelWriterSheets [str (outpath )])+ 1 ))) :
33+ ## If there's room to just put a number on the end (take the number of existing sheets plus 1)
34+ test_sheet_name = test_sheet_name + str (len (excelWriterSheets [str (outpath )])+ 1 )
35+ print ("sheet name is defined sheet name with number appended" ,test_sheet_name )
36+ else :
37+ ## Otherwise, revert to just calling Sheet plus numbe of sheets (including this new one)
38+ test_sheet_name = "Sheet" + str (len (excelWriterSheets [str (outpath )])+ 1 )
39+ print ("sheet name has reverted to," ,test_sheet_name )
40+ ## Call the function recursively to check the name we've decided on is unique in the workbook
41+ new_sheet_name = test_sheet_name
42+ test_sheet_name = check_sheet_name_unique (new_sheet_name ,call_count = call_count + 1 )
43+ return test_sheet_name ;
44+ else :
45+ print ("no entry for sheetname in excelWriterSheets list for current filepath, returning sheet name" ,test_sheet_name )
46+ return test_sheet_name ;
47+ else :
48+ print ("no entry for filepath in excelWriterSheets, returning sheet name" ,test_sheet_name )
49+ return test_sheet_name ;
50+ else :
51+ error_message = f"could not create unique name for worksheet { worksheet } !r in file { outpath } !r within 3 attempts"
52+ raise RuntimeError (error_message )
53+
2454## First, prepare regular expression to be used to pull required info out of record description, the bits with (?P<some_name>...) allow us to refer to bits of the description by name
2555## note though that to match original analysis we actually only need Addressees as places is already returned as a distinct field in the JSON.
2656## used in get_addressees function defined below.
160190 if "excel_sheet_name" in row :
161191 if row ["excel_sheet_name" ] :
162192 sheet_name = row .pop ("excel_sheet_name" )
193+ ## Remove characters
163194 if not (current_output_filepath .suffix == ".xls" or current_output_filepath .suffix == ".xlsx" ) :
164195 print ("Output file is not an Excel spreadsheet, sheet name will be ignored." )
196+ else :
197+ for char in r"*|\/?:[]" :
198+ if char in sheet_name :
199+ sheet_name = sheet_name .replace (char ,"" )
200+ print ("removed unsupported character" ,char ,"from given sheet_name" )
201+ if len (sheet_name ) > 30 :
202+ sheet_name = sheet_name [0 :31 ]
203+ print ("supplied sheet name was too long: truncated to" ,sheet_name )
204+ sheet_name = check_sheet_name_unique (sheet_name )
205+ print ("Final sheet name" ,sheet_name )
165206 else :
166207 sheet_name = None
167208 del row ["excel_sheet_name" ]
@@ -402,8 +443,10 @@ def other_possible_labels(v) :
402443 outputmode = "a"
403444 else :
404445 outputmode = "w"
405-
406- if outpath .suffix in [".xls" ,".xlsx" ] :
446+
447+ print ("output mode" ,outputmode )
448+
449+ if outpath .suffix .lower () in [".xls" ,".xlsx" ] :
407450 ## We want an actual Excel file,not CSV. Pandas docs suggested engine would be found automagically based on extension, but that didn't seem
408451 ## to work, so explicitly set engine for ourselves.
409452 if outpath .suffix == ".xls" :
@@ -424,7 +467,7 @@ def other_possible_labels(v) :
424467 ## We're adding to existing Excel file (in memory), so add a new sheet to the list for the current outpath, either the supplied name or
425468 ## just Sheetn+1, where n is the number of sheets already in the list for this outpath
426469 if sheet_name :
427- excelWriterSheets [str (outpath )]= [ sheet_name ]
470+ excelWriterSheets [str (outpath )]. append ( sheet_name )
428471 else :
429472 excelWriterSheets [str (outpath )].append ("Sheet" + str (len (excelWriterSheets [str (outpath )])+ 1 ))
430473 ## Create the Excel sheet on the relevant writer
0 commit comments