Working with VBA is usually not considered high level of programming. Still, when it comes to Excel, this is probably the best solution. However, Python has some libraries which can help a bit, if you are willing to “mimic” the VBA capabilities.
In this article I present a code snippet, that writes some simple maths calculations in all the sheets of an existing Excel file.
1 2 3 4 5 6 7 8 9 10 |
import openpyxl wbkName = 'newFile.xlsx' wbk = openpyxl.load_workbook(wbkName) for wks in wbk.worksheets: for myRow in range(1, 100): for myCol in range(1,100): wks.cell(row=myRow, column=myCol).value = myRow + myCol wbk.save(wbkName) wbk.close |
The three nested loops do the following – the first one loops the “worksheets collection” of the existing worksheets of the workbook “newFile.xlsx”. The second and the third one loop through rows and columns and write values in the Excel file. The saving of the workbook is a must.
Small and easy. Still, it works.