excel 中的公式是在电子表格中对数据进行计算的方程或表达式。它们允许您执行基本的算术操作,如加法、减法、乘法和除法,以及更高级的函数,如统计分析、日期和时间计算以及逻辑评估。通过将公式纳入到您的excel电子表格中,您可以节省时间,消除错误,并从数据中获得有价值的见解。在本文中,我们将演示如何使用 spire.xls for python 在 python 中添加或读取 excel 中的公式。
安装 spire.xls for python
本教程需要 spire.xls for python 和 plum-dispatch v1.7.4。您可以通过以下 pip 命令将它们轻松安装到 vs code 中。
pip install spire.xls
如果您不确定如何安装,请参考此教程: 如何在 vs code中安装 spire.xls for python
python 向 excel 添加公式
spire.xls for python 提供了 worksheet.range[rowindex, columnindex].formula 属性,用于向 excel 工作表的特定单元格添加公式。详细步骤如下:
- 创建 workbook 类的对象。
- 使用 workbook.worksheets[sheetindex] 属性获取所需的工作表。
- 使用 worksheet.range[rowindex, columnindex].text 和 worksheet.range[rowindex, columnindex].numbervalue 属性将一些文本和数值数据添加到工作表的特定单元格。
- 使用 worksheet.range[rowindex, columnindex].text 和 worksheet.range[rowindex, columnindex].formula 属性向工作表的特定单元格添加文本和公式。
- 使用 workbook.savetofile() 方法保存结果文件。
- python
from spire.xls import *
from spire.xls.common import *
# 创建一个workbook对象
workbook = workbook()
# 获取第一个工作表
sheet = workbook.worksheets[0]
# 当前行数和公式变量初始化
currentrow = 1
currentformula = ""
# 在单元格中设置文本并设置样式
sheet.range[currentrow, 1].text = "测试数据:"
sheet.range[currentrow, 1].style.font.isbold = true
sheet.range[currentrow, 1].style.fillpattern = excelpatterntype.solid
sheet.range[currentrow, 1].style.knowncolor = excelcolors.lightgreen1
sheet.range[currentrow, 1].style.borders[borderslinetype.edgebottom].linestyle = linestyletype.medium
currentrow = 1
# 在单元格中设置数字值
sheet.range[currentrow, 1].numbervalue = 7.3
sheet.range[currentrow, 2].numbervalue = 5
sheet.range[currentrow, 3].numbervalue = 8.2
sheet.range[currentrow, 4].numbervalue = 4
sheet.range[currentrow, 5].numbervalue = 3
sheet.range[currentrow, 6].numbervalue = 11.3
currentrow = 2
# 设置公式标题行的样式
sheet.range[currentrow, 1].text = "公式"
sheet.range[currentrow, 2].text = "计算结果"
sheet.range[currentrow, 1, currentrow, 2].style.font.isbold = true
sheet.range[currentrow, 1, currentrow, 2].style.knowncolor = excelcolors.lightgreen1
sheet.range[currentrow, 1, currentrow, 2].style.fillpattern = excelpatterntype.solid
sheet.range[currentrow, 1, currentrow, 2].style.borders[borderslinetype.edgebottom].linestyle = linestyletype.medium
currentrow = 1
#添加文本和公式到工作表
# 文本
currentformula = "=\"hello\""
sheet.range[currentrow, 1].text = "'" currentformula
sheet.range[currentrow, 2].formula = currentformula
currentrow = 1
# int
currentformula = "=300"
sheet.range[currentrow, 1].text = "'" currentformula
sheet.range[currentrow, 2].formula = currentformula
currentrow = 1
# float
currentformula = "=3389.639421"
sheet.range[currentrow, 1].text = "'" currentformula
sheet.range[currentrow, 2].formula = currentformula
currentrow = 1
# bool
currentformula = "=false"
sheet.range[currentrow, 1].text = "'" currentformula
sheet.range[currentrow, 2].formula = currentformula
currentrow = 1
# 表达式
currentformula = "=1 2 3 4 5-6-7 8-9"
sheet.range[currentrow, 1].text = "'" currentformula
sheet.range[currentrow, 2].formula = currentformula
currentrow = 1
currentformula = "=33*3/4-2 10"
sheet.range[currentrow, 1].text = "'" currentformula
sheet.range[currentrow, 2].formula = currentformula
currentrow = 1
# 单元格引用
currentformula = "=sheet1!$b$2"
sheet.range[currentrow, 1].text = "'" currentformula
sheet.range[currentrow, 2].formula = currentformula
currentrow = 1
# 函数
# 使用average函数计算平均值
currentformula = "=average(sheet1!$d$2:f$2)"
sheet.range[currentrow, 1].text = "'" currentformula
sheet.range[currentrow, 2].formula = currentformula
currentrow = 1
# 使用count函数计算数字个数
currentformula = "=count(3,5,8,10,2,34)"
sheet.range[currentrow, 1].text = "'" currentformula
sheet.range[currentrow, 2].formula = currentformula
currentrow = 1
# 使用now函数获取当前日期和时间
currentformula = "=now()"
sheet.range[currentrow, 1].text = "'" currentformula
sheet.range[currentrow, 2].formula = currentformula
sheet.range[currentrow, 2].style.numberformat = "yyyy-mm-dd"
currentrow = 1
# 使用second函数获取时间的秒数部分
currentformula = "=second(0.503)"
sheet.range[currentrow, 1].text = "'" currentformula
sheet.range[currentrow, 2].formula = currentformula
currentrow = 1
# 使用minute函数获取时间的分钟部分
currentformula = "=minute(0.78125)"
sheet.range[currentrow, 1].text = "'" currentformula
sheet.range[currentrow, 2].formula = currentformula
currentrow = 1
# 使用month函数获取月份值
currentformula = "=month(9)"
sheet.range[currentrow, 1].text = "'" currentformula
sheet.range[currentrow, 2].formula = currentformula
currentrow = 1
# 使用day函数获取日期的天数部分
currentformula = "=day(10)"
sheet.range[currentrow, 1].text = "'" currentformula
sheet.range[currentrow, 2].formula = currentformula
currentrow = 1
# 使用time函数创建时间值
currentformula = "=time(4,5,7)"
sheet.range[currentrow, 1].text = "'" currentformula
sheet.range[currentrow, 2].formula = currentformula
currentrow = 1
# 使用date函数创建日期值
currentformula = "=date(6,4,2)"
sheet.range[currentrow, 1].text = "'" currentformula
sheet.range[currentrow, 2].formula = currentformula
currentrow = 1
# 使用rand函数生成随机数
currentformula = "=rand()"
sheet.range[currentrow, 1].text = "'" currentformula
sheet.range[currentrow, 2].formula = currentformula
currentrow = 1
# 使用hour函数获取时间的小时部分
currentformula = "=hour(0.5)"
sheet.range[currentrow, 1].text = "'" currentformula
sheet.range[currentrow, 2].formula = currentformula
currentrow = 1
# 使用mod函数计算两个数的取模
currentformula = "=mod(5,3)"
sheet.range[currentrow, 1].text = "'" currentformula
sheet.range[currentrow, 2].formula = currentformula
currentrow = 1
# 使用weekday函数获取日期的星期几
currentformula = "=weekday(3)"
sheet.range[currentrow, 1].text = "'" currentformula
sheet.range[currentrow, 2].formula = currentformula
currentrow = 1
# 使用year函数获取年份值
currentformula = "=year(23)"
sheet.range[currentrow, 1].text = "'" currentformula
sheet.range[currentrow, 2].formula = currentformula
currentrow = 1
# 使用not函数对逻辑值取反
currentformula = "=not(true)"
sheet.range[currentrow, 1].text = "'" currentformula
sheet.range[currentrow, 2].formula = currentformula
currentrow = 1
# 使用or函数进行逻辑或运算
currentformula = "=or(true)"
sheet.range[currentrow, 1].text = "'" currentformula
sheet.range[currentrow, 2].formula = currentformula
currentrow = 1
# 使用and函数进行逻辑与运算
currentformula = "=and(true)"
sheet.range[currentrow, 1].text = "'" currentformula
sheet.range[currentrow, 2].formula = currentformula
currentrow = 1
# 使用value函数将文本转换为数值
currentformula = "=value(30)"
sheet.range[currentrow, 1].text = "'" currentformula
sheet.range[currentrow, 2].formula = currentformula
currentrow = 1
# 使用len函数获取文本的长度
currentformula = "=len(\"world\")"
sheet.range[currentrow, 1].text = "'" currentformula
sheet.range[currentrow, 2].formula = currentformula
currentrow = 1
# 使用mid函数从文本中提取子字符串
currentformula = "=mid(\"world\",4,2)"
sheet.range[currentrow, 1].text = "'" currentformula
sheet.range[currentrow, 2].formula = currentformula
currentrow = 1
# 使用round函数对数值进行四舍五入
currentformula = "=round(7,3)"
sheet.range[currentrow, 1].text = "'" currentformula
sheet.range[currentrow, 2].formula = currentformula
currentrow = 1
# 使用sign函数获取数值的符号
currentformula = "=sign(4)"
sheet.range[currentrow, 1].text = "'" currentformula
sheet.range[currentrow, 2].formula = currentformula
currentrow = 1
# 使用int函数获取数值的整数部分
currentformula = "=int(200)"
sheet.range[currentrow, 1].text = "'" currentformula
sheet.range[currentrow, 2].formula = currentformula
currentrow = 1
# 使用abs函数获取数值的绝对值
currentformula = "=abs(-1.21)"
sheet.range[currentrow, 1].text = "'" currentformula
sheet.range[currentrow, 2].formula = currentformula
currentrow = 1
# 使用ln函数获取数值的自然对数
currentformula = "=ln(15)"
sheet.range[currentrow, 1].text = "'" currentformula
sheet.range[currentrow, 2].formula = currentformula
currentrow = 1
# 使用exp函数计算指数值
currentformula = "=exp(20)"
sheet.range[currentrow, 1].text = "'" currentformula
sheet.range[currentrow, 2].formula = currentformula
currentrow = 1
# 使用sqrt函数计算平方根
currentformula = "=sqrt(40)"
sheet.range[currentrow, 1].text = "'" currentformula
sheet.range[currentrow, 2].formula = currentformula
currentrow = 1
# 使用pi函数获取圆周率值
currentformula = "=pi()"
sheet.range[currentrow, 1].text = "'" currentformula
sheet.range[currentrow, 2].formula = currentformula
currentrow = 1
# 使用cos函数计算余弦值
currentformula = "=cos(9)"
sheet.range[currentrow, 1].text = "'" currentformula
sheet.range[currentrow, 2].formula = currentformula
currentrow = 1
# 使用sin函数计算正弦值
currentformula = "=sin(45)"
sheet.range[currentrow, 1].text = "'" currentformula
sheet.range[currentrow, 2].formula = currentformula
currentrow = 1
# 使用max函数获取数值的最大值
currentformula = "=max(10,30)"
sheet.range[currentrow, 1].text = "'" currentformula
sheet.range[currentrow, 2].formula = currentformula
currentrow = 1
# 使用min函数获取数值的最小值
currentformula = "=min(5,7)"
sheet.range[currentrow, 1].text = "'" currentformula
sheet.range[currentrow, 2].formula = currentformula
currentrow = 1
# 使用average函数计算数值的平均值
currentformula = "=average(12,45)"
sheet.range[currentrow, 1].text = "'" currentformula
sheet.range[currentrow, 2].formula = currentformula
currentrow = 1
# 使用sum函数计算数值的总和
currentformula = "=sum(18,29)"
sheet.range[currentrow, 1].text = "'" currentformula
sheet.range[currentrow, 2].formula = currentformula
currentrow = 1
# 使用if函数进行条件判断
currentformula = "=if(4,2,2)"
sheet.range[currentrow, 1].text = "'" currentformula
sheet.range[currentrow, 2].formula = currentformula
currentrow = 1
# 使用subtotal函数计算子总计
currentformula = "=subtotal(3,sheet1!a2:f2)"
sheet.range[currentrow, 1].text = "'" currentformula
sheet.range[currentrow, 2].formula = currentformula
currentrow = 1
# 设置第一列宽度为32
sheet.setcolumnwidth(1, 32)
# 设置第二列宽度为16
sheet.setcolumnwidth(2, 16)
# 设置第三列宽度为16
sheet.setcolumnwidth(3, 16)
# 创建一个名为"style"的样式并设置水平对齐方式为左对齐
style = workbook.styles.add("style")
style.horizontalalignment = horizontalaligntype.left
# 应用样式到工作表
sheet.applystyle(style)
# 将工作簿保存为"添加公式.xlsx",文件格式为excel 2016
workbook.savetofile("添加公式.xlsx", excelversion.version2016)
workbook.dispose()
python 读取 excel 中的公式
要读取 excel 工作表中的公式,您需要通过循环遍历工作表中的所有单元格,然后使用 cell.hasformula 属性查找包含公式的单元格,并使用 cellrange.formula 属性获取这些单元格的公式。具体步骤如下:
- 创建 workbook 类的对象。
- 使用 workbook.loadfromfile() 方法加载 excel 文件。
- 使用 workbook.worksheets[sheetindex] 属性获取所需的工作表。
- 使用 worksheet.allocatedrange 属性获取工作表的使用范围。
- 创建一个空列表。
- 遍历使用范围内的所有单元格。
- 使用 cell.hasformula 属性查找包含公式的单元格。
- 使用 cellrange.rangeaddresslocal 和 cellrange.formula 属性获取单元格的名称和公式。
- 将单元格名称和公式追加到列表中。
- 将列表中的项写入文本文件中。
- python
from spire.xls import *
from spire.xls.common import *
# 创建一个新的工作簿对象
workbook = workbook()
# 从文件加载工作簿数据
workbook.loadfromfile("添加公式.xlsx")
# 获取第一个工作表
sheet = workbook.worksheets[0]
# 获取工作表中已使用的单元格范围
usedrange = sheet.allocatedrange
# 创建一个空列表,用于存储带有公式的单元格信息
list = []
# 遍历工作表中的每个单元格
for cell in usedrange:
# 检查单元格是否包含公式
if(cell.hasformula):
# 获取单元格地址和公式内容
cellname = cell.rangeaddresslocal
formula = cell.formula
# 将单元格地址和公式内容添加到列表中
list.append(cellname " 有一个公式: " formula)
# 打开一个名为"公式.txt"的文本文件,以写入模式和utf-8编码方式打开
with open("公式.txt", "w", encoding="utf-8") as text_file:
# 遍历列表中的每个项,并将其写入文本文件中
for item in list:
text_file.write(item "\n")
workbook.dispose()
申请临时 license
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用javascript。获取有效期 30 天的临时许可证。