Python Program for Trigonometric Operations in Excel File using openpyxl

Prerequisite: Using openpyxl to adjust the rows and columns of an excel sheet.

openpyxl Module:

openpyxl is a Python library that allows you to do various actions on Excel files such as reading, writing, mathematical operations, and graph plotting. Let’s look at how to use openpyxl to execute various trigonometric computations.

Program for Trigonometric Operations in Excel File using openpyxl in Python

Trigonometric Functions:

  • =SIN(num): This function returns the sine of an angle. The angle in radians for which you want the sine is represented by the number.
  • =COS(num):  The cosine of an angle is returned.
  • =TAN(num):  The tangent of an angle is returned.
  • =CSC(num):  The cosecant of an angle is returned.
  • =SEC(num): This function returns the secant of an angle.
  • =COT(num): This function returns the cotangent of an angle.

Example1

Approach:

  • Import openpyxl module using the import keyword.
  • Call the Workbook() function to create a workbook object and store it in a variable.
  • Choose the active sheet using the active attribute and store it in another variable.
  • Set the column’s width using the column_dimensions.width to samoe random values.
  • Writing for the first 7 cells of the Excel by passing some random values to it.
  • Applying the trigonometric operations for the first 7 cells.
  • Calculate the trigonometric values for the above 7 cells C1 to C7 using the functions =SIN, =COS, =TAN, =CSC, =SEC and =COT for the above-given radian values.
  • Save the above Excel workbook using the save() function by passing the Excel file path to it.
  • The Exit of the Program.

Below is the implementation:

# Import openpyxl module using the import keyword
import openpyxl
   
# Call the Workbook() function to create a workbook object and store it in a variable.
work_book = openpyxl.Workbook()
  
# Choose the active sheet using the active attribute and store it in another variable.
actve_sheet = work_book.active
  
# Set the column's width using the column_dimensions.width to samoe random values
actve_sheet.column_dimensions['A'].width = 30
actve_sheet.column_dimensions['B'].width = 30
actve_sheet.column_dimensions['C'].width = 40
  
# Writing for the first 7 cells of the Excel by passing some random values to it
actve_sheet['A1'] = "Angles(radians)"
actve_sheet['A2'] = 0.6
actve_sheet['A3'] = 0.5
actve_sheet['A4'] = 0.4
actve_sheet['A5'] = 0.3
actve_sheet['A6'] = 0.2
actve_sheet['A7'] = 0.1

  
# Applying the trigonometric operations for the first 7 cells
actve_sheet['B1'] = "Trigonometric Functions"
actve_sheet['B2'] = "Sine"
actve_sheet['B3'] = "Cosine"
actve_sheet['B4'] = "Tangent"
actve_sheet['B5'] = "Cosecant"
actve_sheet['B6'] = "Secant"
actve_sheet['B7'] = "Cotangent"
  
# Calculate the trigonometric values for the above 7 cells C1 to C7 using the functions
# =SIN, =COS, =TAN, =CSC, =SEC and =COT for the above given radian values
actve_sheet['C1'] = 'Trigonometric values'
actve_sheet['C2'] = "= SIN(0.6)"
actve_sheet['C3'] = '= COS(0.5)'
actve_sheet['C4'] = '= TAN(0.4)'
actve_sheet['C5'] = '= CSC(0.3)'
actve_sheet['C6'] = '= SEC(0.2)'
actve_sheet['C7'] = '= COT(0.1)'

# Save the above Excel workbook using the save() function by passing the Excel file path to it.
work_book.save("demo.xlsx")

Output:

Hyperbolic Trigonometric Functions:

  • =SINH(num): This function returns the hyperbolic sine of a Number.
  • =COSH(num): A Number’s hyperbolic cosine is returned
  • =TANH(num): A Number’s hyperbolic tangent is returned
  • =CSCH(num): A Number’s hyperbolic cosecant is returned
  • =SECH(num): A Number’s hyperbolic secant is returned
  • =COTH(num): A Number’s hyperbolic cotangent is returned

Example2

Approach:

  • Import openpyxl module using the import keyword.
  • Call the Workbook() function to create a workbook object and store it in a variable.
  • Choose the active sheet using the active attribute and store it in another variable.
  • Set the column’s width using the column_dimensions.width to samoe random values.
  • Writing for the first 7 cells of the Excel by passing some random values to it.
  • Applying the Hyperbolic trigonometric operations for the first 7 cells.
  • Calculate the trigonometric values for the above 7 cells C1 to C7 using the functions =SINH, =COSH, =TANH, =CSCH, =SECH and =COTH for the above-given radian values.
  • Save the above Excel workbook using the save() function by passing the Excel file path to it.
  • The Exit of the Program.

Below is the implementation:

# Import openpyxl module using the import keyword
import openpyxl
   
# Call the Workbook() function to create a workbook object and store it in a variable.
work_book = openpyxl.Workbook()
  
# Choose the active sheet using the active attribute and store it in another variable.
actve_sheet = work_book.active
  
# Set the column's width using the column_dimensions.width to samoe random values
actve_sheet.column_dimensions['A'].width = 30
actve_sheet.column_dimensions['B'].width = 30
actve_sheet.column_dimensions['C'].width = 40
  
# Writing for the first 7 cells of the Excel by passing some random values to it
actve_sheet['A1'] = "Angles(radians)"
actve_sheet['A2'] = 0.6
actve_sheet['A3'] = 0.5
actve_sheet['A4'] = 0.4
actve_sheet['A5'] = 0.3
actve_sheet['A6'] = 0.2
actve_sheet['A7'] = 0.1

  
# Applying the Hyperbolic trigonometric operations for the first 7 cells
actve_sheet['B1'] = "Hyperbolic Trigonometric Functions"
actve_sheet['B2'] = "Hyperbolic Sine"
actve_sheet['B3'] = "Hyperbolic Cosine"
actve_sheet['B4'] = "Hyperbolic Tangent"
actve_sheet['B5'] = "Hyperbolic Cosecant"
actve_sheet['B6'] = "Hyperbolic Secant"
actve_sheet['B7'] = "Hyperbolic Cotangent"
  
# Calculate the Hyperbolic trigonometric values for the above 7 cells C1 to C7 using the functions
# =SINH, =COSH, =TANH, =CSCH, =SECH and =COTH for the above given radian values
actve_sheet['C1'] = 'Hyperbolic Trigonometric values'
actve_sheet['C2'] = '= SINH(0.6)'
actve_sheet['C3'] = '= COSH(0.5)'
actve_sheet['C4'] = '= TANH(0.4)'
actve_sheet['C5'] = '= CSCH(0.3)'
actve_sheet['C6'] = '= SECH(0.2)'
actve_sheet['C7'] = '= COTH(0.1)'

# Save the above Excel workbook using the save() function by passing the Excel file path to it.
work_book.save("demo.xlsx")

Output:

 

Leave a Comment