I need to be able to run a macro on 150 csv files that double clicks every non empty cell in the worksheet. I need this because the files I have contain a custom date format that excel does not recognize unless I double click the cell. Because I have 150 worksheets each with thousands of columns and rows, I need to inject a macro from python, since I am using python. To inject the macro I am using the following code which I copied from a different post (Use Python to Inject Macros into Spreadsheets); I made some modifications to suit my setup. The problem is that if I run the VBA code in excel, it works, but if I inject it from python the macro does nothing. I think that this has something to do with how I converted my files from dat to csv. This is the code I used.
for j in range(len(dat_names)):
# to record elapsed runtime
archive_member_cycler_start = time.time()
archive_members_list.extract(dat_names[j],destination)
with open('%s.dat' % dat_names[j][:-4], 'r', encoding='ISO-8859-1') as input_file:
# remove | characters from the data
newLines = []
for line in input_file:
newLine = [x.strip() for x in line.split('|')]
# remove ^ characters from the data also; they are the last column
newLine = newLine[:-3]
newLines.append(newLine)
with open('%s.csv' % dat_names[j][:-4], 'w', newline='') as output_file:
file_writer = csv.writer(output_file)
try:
file_writer.writerows(newLines)
archive_member_cycler_end = time.time()
archive_member_cycler_run = archive_member_cycler_end-archive_member_cycler_start
a = '%s: %s.dat converted to csv (%.3f)' % (newpath[i], dat_names[j][:-4], archive_member_cycler_run)
print(a)
other.append(a)
# remove dat file because dont want it
os.remove('%s.dat' % dat_names[j][:-4])
except Exception as e:
print(e)
pfn = str(newpath[i]+': '+dat_names[j][:-4])
archive_member_cycler_end = time.time()
archive_member_cycler_run = archive_member_cycler_end-archive_member_cycler_start
a = '%s: %s.dat converted to csv WITH ERROR (%.3f)\n' % (newpath[i], dat_names[j][:-4], archive_member_cycler_run);
print(a)
other.append(a)
# compress the problematic dat file
zipper = zipfile.ZipFile('%s.zip' % dat_names[j][:-4], 'w')
zipper.write('%s.dat' % dat_names[j][:-4], compress_type=zipfile.ZIP_DEFLATED)
zipper.close()
# remove the dat file because dont want it
os.remove('%s.dat' % dat_names[j][:-4])
pass
Any ideas on how to make the macro work?