Python|使用openpyxl在excel文件中进行三角函数运算
先决条件:使用openpyxl调整excel表格的行和列。
Openpyxl
是一个Python库,使用它可以对 Excel 文件执行多种操作,例如读取、写入、数学运算和绘图。让我们看看如何使用 openpyxl 执行不同的三角运算。
简单的三角函数:
代码#1:在程序中使用简单的三角函数。
- =SIN(Number) :返回角度的正弦值。数字是您想要正弦的弧度角。
- =COS(Number) :返回角度的余弦值。
- =TAN(Number) :返回角度的正切。
- =CSC(Number) :返回角度的余割。
- =SEC(Number) :返回角度的割线。
- =COT(Number) :返回角度的余切。
# import openpyxl module
import openpyxl
# Call a Workbook() function of openpyxl
# to create a new blank Workbook object
wb = openpyxl.Workbook()
# Get workbook active sheet
# from the active attribute.
sheet = wb.active
# set the width of the column
sheet.column_dimensions['A'].width = 20
sheet.column_dimensions['B'].width = 30
sheet.column_dimensions['C'].width = 20
# writing to the cell of an excel sheet
sheet['A1'] = "angles in radian"
sheet['A2'] = 0.1
sheet['A3'] = 0.2
sheet['A4'] = 0.3
sheet['A5'] = 0.4
sheet['A6'] = 0.5
sheet['A7'] = 0.6
# mention performing trigonometric operations
sheet['B1'] = "Applying trigonometric function"
sheet['B2'] = "Sine"
sheet['B3'] = "Cosine"
sheet['B4'] = "Tangent"
sheet['B5'] = "Cosecant"
sheet['B6'] = "Secant"
sheet['B7'] = "Cotangent"
# The value in cell C1 to C7 is set to a formula
# that calculates values for particular radian.
sheet['C1'] = 'corresponding values'
sheet['C2'] = '= SIN(0.1)'
sheet['C3'] = '= COS(0.2)'
sheet['C4'] = '= TAN(0.3)'
sheet['C5'] = '= CSC(0.4)'
sheet['C6'] = '= SEC(0.5)'
sheet['C7'] = '= COT(0.6)'
# save the file
wb.save("simple_trigonometric.xlsx")
输出:
代码#2:在程序中使用双曲三角函数。
- =SINH(Number) :返回数字的双曲正弦值。
- =COSH(Number) :返回数字的双曲余弦值。
- =TANH(number) :返回数字的双曲正切。
- =CSCH(Number) :返回数字的双曲余割。
- =SECH(Number) :返回数字的双曲割线。
- =COTH(Number) :返回数字的双曲余切值。
# import openpyxl module
import openpyxl
# Call a Workbook() function of openpyxl
# to create a new blank Workbook object
wb = openpyxl.Workbook()
# Get workbook active sheet
# from the active attribute.
sheet = wb.active
# set the width of the column
sheet.column_dimensions['A'].width = 20
sheet.column_dimensions['B'].width = 30
sheet.column_dimensions['C'].width = 20
# writing to the cell of an excel sheet
sheet['A1'] = "angles in radian"
sheet['A2'] = 0.1
sheet['A3'] = 0.2
sheet['A4'] = 0.3
sheet['A5'] = 0.4
sheet['A6'] = 0.5
sheet['A7'] = 0.6
# mention performing trigonometric operations
sheet['B1'] = "Applying trigonometric function"
sheet['B2'] = "Hyperbolic Sine"
sheet['B3'] = "Hyperbolic Cosine"
sheet['B4'] = "Hyperbolic Tangent"
sheet['B5'] = "Hyperbolic Cosecant"
sheet['B6'] = "Hyperbolic Secant"
sheet['B7'] = "Hyperbolic Cotangent"
# The value in cell C1 to C7 is set to a formula
# that calculates values for particular radian.
sheet['C1'] = 'corresponding values'
sheet['C2'] = '= SINH(0.1)'
sheet['C3'] = '= COSH(0.2)'
sheet['C4'] = '= TANH(0.3)'
sheet['C5'] = '= CSCH(0.4)'
sheet['C6'] = '= SECH(0.5)'
sheet['C7'] = '= COTH(0.6)'
# save the file
wb.save("Hyperbolic_trigonometric.xlsx")
输出: