- Python XlsxWriter 教程
- Python XlsxWriter - 首页
- Python XlsxWriter - 概述
- Python XlsxWriter - 环境设置
- Python XlsxWriter - Hello World
- Python XlsxWriter - 重要类
- Python XlsxWriter - 单元格表示法与范围
- Python XlsxWriter - 定义名称
- Python XlsxWriter - 公式与函数
- Python XlsxWriter - 日期和时间
- Python XlsxWriter - 表格
- Python XlsxWriter - 应用筛选器
- Python XlsxWriter - 字体与颜色
- Python XlsxWriter - 数字格式
- Python XlsxWriter - 边框
- Python XlsxWriter - 超链接
- Python XlsxWriter - 条件格式化
- Python XlsxWriter - 添加图表
- Python XlsxWriter - 图表格式化
- Python XlsxWriter - 图表图例
- Python XlsxWriter - 条形图
- Python XlsxWriter - 折线图
- Python XlsxWriter - 饼图
- Python XlsxWriter - Sparklines (迷你图)
- Python XlsxWriter - 数据验证
- Python XlsxWriter - 大纲与分组
- Python XlsxWriter - 冻结与拆分窗格
- Python XlsxWriter - 隐藏/保护工作表
- Python XlsxWriter - 文本框
- Python XlsxWriter - 插入图片
- Python XlsxWriter - 页面设置
- Python XlsxWriter - 页眉与页脚
- Python XlsxWriter - 单元格注释
- Python XlsxWriter - 与 Pandas 协同工作
- Python XlsxWriter - VBA 宏
- Python XlsxWriter 有用资源
- Python XlsxWriter - 快速指南
- Python XlsxWriter - 有用资源
- Python XlsxWriter - 讨论
Python XlsxWriter - 公式与函数
Worksheet 类提供了三种使用公式的方法。
- write_formula()
- write_array_formula()
- write_dynamic_array_formula()
所有这些方法都用于为单元格分配公式以及函数。
write_formula() 方法
write_formula() 方法需要单元格的地址,以及包含有效 Excel 公式的字符串。在公式字符串内部,仅接受 A1 样式地址表示法。但是,单元格地址参数可以是标准 Excel 类型或基于零的行和列号表示法。
示例
在下面的示例中,各种语句使用 write_formula() 方法。第一个使用标准 Excel 表示法分配公式。第二个语句使用行和列号来指定目标单元格的地址,在该单元格中设置公式。在第三个示例中,IF() 函数被分配给 G2 单元格。
import xlsxwriter
wb = xlsxwriter.Workbook('hello.xlsx')
ws = wb.add_worksheet()
data=[
['Name', 'Phy', 'Che', 'Maths', 'Total', 'percent', 'Result' ],
['Arvind', 50,60,70]
]
ws.write_row('A1', data[0])
ws.write_row('A2', data[1])
ws.write_formula('E2', '=B2+C2+D2')
ws.write_formula(1,5, '=E2*100/300')
ws.write_formula('G2', '=IF(F2>=50, "PASS","FAIL")')
wb.close()
输出
Excel 文件显示以下结果:
write_array_formula() 方法
write_array_formula() 方法用于将公式扩展到一个范围。在 Excel 中,数组公式对一组值执行计算。它可以返回单个值或一系列值。
数组公式用公式周围的一对花括号表示 - {=SUM(A1:B1*A2:B2)}。范围可以通过范围中第一个和最后一个单元格的行和列号来指定(例如 0,0, 2,2),也可以通过字符串表示形式 'A1:C2' 来指定。
示例
在下面的示例中,数组公式用于 E、F 和 G 列,以根据 B2:D4 范围内的分数计算总分、百分比和结果。
import xlsxwriter
wb = xlsxwriter.Workbook('hello.xlsx')
ws = wb.add_worksheet()
data=[
['Name', 'Phy', 'Che', 'Maths', 'Total', 'percent', 'Result'],
['Arvind', 50,60,70],
['Amar', 55,65,75],
['Asha', 75,85,80]
]
for row in range(len(data)):
ws.write_row(row,0, data[row])
ws.write_array_formula('E2:E4', '{=B2:B4+C2:C4+D2:D4}')
ws.write_array_formula(1,5,3,5, '{=(E2:E4)*100/300}')
ws.write_array_formula('G2:G4', '{=IF((F2:F4)>=50, "PASS","FAIL")}')
wb.close()
输出
以下是使用 MS Excel 打开工作表时的显示方式:
write_dynamic_array_data() 方法
write_dynamic_array_data() 方法将动态数组公式写入单元格范围。动态数组的概念已在 EXCEL 的 365 版本中引入,并且引入了一些利用动态数组优势的新函数。这些函数是:
| 序号 | 函数及描述 |
|---|---|
| 1 | FILTER 筛选数据并返回匹配的记录 |
| 2 | RANDARRAY 生成随机数数组 |
| 3 | SEQUENCE 生成一系列数字的数组 |
| 4 | SORT 按列排序范围 |
| 5 | SORTBY 按另一个范围或数组排序范围 |
| 6 | UNIQUE 从列表或范围中提取唯一值 |
| 7 | XLOOKUP VLOOKUP 的替代 |
| 8 | XMATCH MATCH 函数的替代 |
动态数组是返回值范围,其大小可以根据结果更改。例如,像 FILTER() 这样的函数返回一个返回值数组,该数组的大小根据筛选结果而变化。
示例
在下面的示例中,数据范围是 A1:D17。筛选器函数使用此范围,条件范围是 C1:C17,其中给出了产品名称。FILTER() 函数导致动态数组,因为满足条件的行数可能会发生变化。
import xlsxwriter
wb = xlsxwriter.Workbook('hello.xlsx')
ws = wb.add_worksheet()
data = (
['Region', 'SalesRep', 'Product', 'Units'],
['East', 'Tom', 'Apple', 6380],
['West', 'Fred', 'Grape', 5619],
['North', 'Amy', 'Pear', 4565],
['South', 'Sal', 'Banana', 5323],
['East', 'Fritz', 'Apple', 4394],
['West', 'Sravan', 'Grape', 7195],
['North', 'Xi', 'Pear', 5231],
['South', 'Hector', 'Banana', 2427],
['East', 'Tom', 'Banana', 4213],
['West', 'Fred', 'Pear', 3239],
['North', 'Amy', 'Grape', 6520],
['South', 'Sal', 'Apple', 1310],
['East', 'Fritz', 'Banana', 6274],
['West', 'Sravan', 'Pear', 4894],
['North', 'Xi', 'Grape', 7580],
['South', 'Hector', 'Apple', 9814])
for row in range(len(data)):
ws.write_row(row,0, data[row])
ws.write_dynamic_array_formula('F1', '=FILTER(A1:D17,C1:C17="Apple")')
wb.close()
输出
请注意,写入 write_dynamic_array_formula() 的公式字符串不需要包含花括号。生成的 hello.xlsx 必须使用 Excel 365 应用程序打开。