📅  最后修改于: 2020-04-11 08:09:30             🧑  作者: Mango
先决条件: 使用openpyxl 调整 Excel工作表的行和列。
Openpyxl
是一个Python库,使用它可以对excel文件进行多种操作,如读取,编写,数学运算和绘制图形。让我们看看如何使用openpyxl执行不同的三角运算。
简单的三角函数:
代码1:在程序中使用简单的三角函数。
# 导入openpyxl模块
import openpyxl
# 调用openpyxl的Workbook()函数以创建一个新的空白Workbook对象
wb = openpyxl.Workbook()
# 从active属性获取工作簿活动表.
sheet = wb.active
# 设置列的宽度
sheet.column_dimensions['A'].width = 20
sheet.column_dimensions['B'].width = 30
sheet.column_dimensions['C'].width = 20
# 写入Excel工作表的单元格
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
# 提到执行三角运算
sheet['B1'] = "Applying trigonometric function"
sheet['B2'] = "Sine"
sheet['B3'] = "Cosine"
sheet['B4'] = "Tangent"
sheet['B5'] = "Cosecant"
sheet['B6'] = "Secant"
sheet['B7'] = "Cotangent"
# 单元格C1至C7中的值设置为公式,该公式计算特定弧度的值.
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)'
# 保存文件
wb.save("simple_trigonometric.xlsx")
输出:
代码2:在程序中使用双曲三角函数。
# 导入openpyxl模块
import openpyxl
# 调用openpyxl的Workbook()函数以创建一个新的空白Workbook对象
wb = openpyxl.Workbook()
# 从active属性获取工作簿活动表.
sheet = wb.active
# 设置列的宽度
sheet.column_dimensions['A'].width = 20
sheet.column_dimensions['B'].width = 30
sheet.column_dimensions['C'].width = 20
# 写入Excel工作表的单元格
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
# 执行三角运算
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"
# 单元格C1至C7中的值设置为公式,该公式计算特定弧度的值.
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")
输出: