Trigonometric Operations on Excel file in Python using openpyxl

In this tutorial, we will be using Python to compute trigonometric functions and hyperbolic trigonometric function values from a given input from an Excel file. We will also write a code that will be able to output all the computed values using basic trigonometric functions and hyperbolic trigonometric functions in the same Excel sheet.

For this, we will be using the Python package Openpyxl. If you haven’t installed openpyxl , please install it with the following command:

pip install Openpyxl

You can also check

Now that the package is installed, we can use it to read values from the excel sheet. I would personally recommend everyone to go through the documentation of openpyxl for more details.

https://openpyxl.readthedocs.io/en/stable/

Accessing the Excel Sheet using Openpyxl

For simplicity, I have created an excel file named ‘Trigonometric.xlsx’. The content of this excel sheet is as follows:

Trigonometric Operation in Excel file in Python using openpyxl

Now, let’s write the Python code for accessing these values using openpyxl package.

# import openpyxl module 
import openpyxl 

# Loading the excel sheet with function load_workbook
wb = openpyxl.load_workbook('Trigonometric.xlsx')  

# First we should make the workbook active
sheet=wb.active

Let’s try to print the values that I have entered in the Excel sheet with Python code.

# sheet.iter_rows basically gets all the rows from the excel
# within the given constraints like min_row,min_col etc.
for row in sheet.iter_rows(min_row=1, min_col=1, max_row=7, max_col=1):
    for cell in row:
        print(cell.value)

OUTPUT:

angles in radian
0.1
0.2
0.3
0.4
0.5
0.6

Let’s create the heading for various functions in the excel sheet.

sheet['B1'] = "Sine"
sheet['C1'] = "Cosine"
sheet['D1'] = "Tangent"
sheet['E1'] = "Cosecant"
sheet['F1'] = "Secant"
sheet['G1'] = "Cotangent"

sheet['H1'] = "Hyp Sine"
sheet['I1'] = "Hyp Cosine"
sheet['J1'] = "Hyp Tangent"
sheet['K1'] = "Hyp Cosecant"
sheet['L1'] = "Hyp Secant"
sheet['M1'] = "Hyp Cotangent"

Trigonometric Operations on an Excel sheet using Openpyxl in Python

Let’s call the inbuilt Python functions for implementing trigonometric and hyperbolic trigonometric functions. For this, I am importing mpmath python package:

# for trigonometric functions and
# for hyperbolic trigonometric functions
import mpmath

Code for assigning the values of various trigonometric functions for the given inputs in the Excel sheet.

i=2
for row in sheet.iter_rows(min_row=1, min_col=1, max_row=7, max_col=1):
    for cell in row:
        if not cell.value=='angles in radian':
            sheet[(f'B{i}'.format(i))]=float(mpmath.sin(cell.value))
            sheet[(f'C{i}'.format(i))]=float(mpmath.cos(cell.value))
            sheet[(f'D{i}'.format(i))]=float(mpmath.tan(cell.value))
            sheet[(f'E{i}'.format(i))]=float(mpmath.csc(cell.value))
            sheet[(f'F{i}'.format(i))]=float(mpmath.sec(cell.value))
            sheet[(f'G{i}'.format(i))]=float(mpmath.cot(cell.value))
            sheet[(f'H{i}'.format(i))]=float(mpmath.sinh(cell.value))
            sheet[(f'I{i}'.format(i))]=float(mpmath.cosh(cell.value))
            sheet[(f'J{i}'.format(i))]=float(mpmath.tanh(cell.value))
            sheet[(f'K{i}'.format(i))]=float(mpmath.csch(cell.value))                  
            sheet[(f'L{i}'.format(i))]=float(mpmath.sech(cell.value))
            sheet[(f'M{i}'.format(i))]=float(mpmath.coth(cell.value))
            i=i+1

         

Saving the workbook with the code:

wb.save("Trigonometric.xlsx")

Now when you open the Excel sheet named ‘Trigonometric.xlsx’ manually, you can see the outputs under each column.

Leave a Reply

Your email address will not be published. Required fields are marked *