I have a 9000 row x 30 column Excel file with content I would like to copy from one Excel workbook to another. The issue I'm facing, however, is that when I run my code, it never stops running. What's more is that the memory usage keeps going up until it's used 90%-98% of my laptops available memory (screenshot).
I tried using timeit
to see how slow my code was, but even that doesn't execute. I refuse to believe it's the size of my Excel file, so it must be an issue with my code. I am unable to pinpoint exactly what it could be though as I have very limited experience with openpyxl
and would therefore greatly appreciate any advice. I'm using openpyxl 3.0.4
and Python 3.8.7
.
def copy(source, destination):
# Open source workbook
wb1 = load_workbook(source)
ws1 = wb1.worksheets[0]
# Open destination workbook
wb2 = load_workbook(destination)
ws2 = wb2.active
# Get rows and columns of source
max_rows = ws1.max_row
max_columns = ws1.max_column
# Copy cells from source to destination
for i in range(1, max_rows + 1):
for j in range(1, max_columns + 1):
# Read from source
c = ws1.cell(row=i, column=j)
# Write to destination file
ws2.cell(row=i, column=j).value = c.value
# Save source
wb2.save(str(destination))
copy_src = r"C:\Users\username\Documents\copy_from.xlsx"
copy_dst = r"C:\Users\username\Documents\copy_to.xlsx"
time = timeit.timeit('%s' % copy(copy_src, copy_dst))
print("Execution time for copy(): %f seconds" % time)