Python | Arithmetic operations in excel file using openpyxl
Prerequisite: Reading & Writing to excel sheet using openpyxl
Openpyxl is a Python library using which one can perform multiple operations on excel files like reading, writing, arithmatic operations and plotting graphs. Let’s see how to perform different arithmatic operations using openpyxl.
- =SUM(cell1:cell2) : Adds all the numbers in a range of cells.
# import openpyxl moduleimportopenpyxl# Call a Workbook() function of openpyxl# to create a new blank Workbook objectwb=openpyxl.Workbook()# Get workbook active sheet# from the active attribute.sheet=wb.active# writing to the cell of an excel sheetsheet['A1']=200sheet['A2']=300sheet['A3']=400sheet['A4']=500sheet['A5']=600# The value in cell A7 is set to a formula# that sums the values in A1, A2, A3, A4, A5 .sheet['A7']='= SUM(A1:A5)'# save the filewb.save("sum.xlsx")chevron_rightfilter_noneOutput:

- =PRODUCT(cell1:cell2) : Multiplies all the numbers in the range of cells.
importopenpyxlwb=openpyxl.Workbook()sheet=wb.activesheet['A1']=2sheet['A2']=3sheet['A3']=4sheet['A4']=5sheet['A5']=6# The value in cell A7 is set to a formula# that multiplies the values in A1, A2, A3, A4, A5 .sheet['A7']='= PRODUCT(A1:A5)'wb.save("product.xlsx")chevron_rightfilter_noneOutput:

- =AVERAGE(cell1:cell2) : It gives the average (arithmetical mean) of all the numbers which is present in the given cell range.
importopenpyxlwb=openpyxl.Workbook()sheet=wb.activesheet['A1']=200sheet['A2']=300sheet['A3']=400sheet['A4']=500sheet['A5']=600# The value in cell A7 is set to a formula# that return average of the values in A1, A2, A3, A4, A5 .sheet['A7']='= AVERAGE(A1:A5)'wb.save("average.xlsx")chevron_rightfilter_noneOutput:

- =QUOTIENT(num1, num2) : It returns the integer portion of a division.
importopenpyxlwb=openpyxl.Workbook()sheet=wb.active# The value in cell is set to a formula# that gives quotient value .sheet['A1']='= QUOTIENT(64, 8)'sheet['A2']='= QUOTIENT(25, 4)'wb.save("quotient.xlsx")chevron_rightfilter_noneOutput:

- =MOD(num1, num2) : Returns the remainder after a number is divided by the divisor.
importopenpyxlwb=openpyxl.Workbook()sheet=wb.active# The value in cell is set to a formula# that gives remainder or modulus value.sheet['A1']='= MOD(64, 8)'sheet['A2']='= MOD(25, 4)'wb.save("modulus.xlsx")chevron_rightfilter_noneOutput:

- =COUNT(cell1:cell2) : It counts the number of cells in a range that contain the number.
importopenpyxlwb=openpyxl.Workbook()sheet=wb.activesheet['A1']=200sheet['A2']=300sheet['A3']=400sheet['A4']=500sheet['A5']=600# The value in cell A7 is set to a formula# that gives counting of number present in the cells.sheet['A7']='= COUNT(A1:A6)'wb.save("count.xlsx")chevron_rightfilter_noneOutput:

Recommended Posts:
- Python | Trigonometric operations in excel file using openpyxl
- Python | Reading an excel file using openpyxl module
- Python | Writing to an excel file using openpyxl module
- Python | Adjusting rows and columns of an excel file using openpyxl module
- Python | Plotting charts in excel sheet using openpyxl module | Set 3
- Python | Plotting charts in excel sheet using openpyxl module | Set – 2
- Python | Plotting charts in excel sheet using openpyxl module | Set - 1
- Reading an excel file using Python
- Python | Excel File Comparison
- Python | Split given list and insert in excel file
- Python | Create and write on excel file using xlsxwriter module
- Arithmetic operations using OpenCV | Python
- Arithmetic Operations on Images using OpenCV | Set-2 (Bitwise Operations on Binary Images)
- Arithmetic Operations on Images using OpenCV | Set-1 (Addition and Subtraction)
- reStructuredText | .rst file to HTML file using Python for Documentations
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



