Excel 中的公式是在电子表格中对数据进行计算的方程或表达式。它们允许您执行基本的算术操作,如加法、减法、乘法和除法,以及更高级的函数,如统计分析、日期和时间计算以及逻辑评估。通过将公式纳入到您的Excel电子表格中,您可以节省时间,消除错误,并从数据中获得有价值的见解。在本文中,我们将演示如何使用 Spire.XLS for Python 在 Python 中添加或读取 Excel 中的公式。
安装 Spire.XLS for Python
本教程需要 Spire.XLS for Python 和 plum-dispatch v1.7.4。您可以通过以下 pip 命令将它们轻松安装到 Windows 中。
pip install Spire.XLS
如果您不确定如何安装,请参考此教程: 如何在 Windows 中安装 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 天的临时许可证。