安转Python包的国内镜像源

清华大学
https://pypi.tuna.tsinghua.edu.cn/simple 阿里云
https://mirrors.aliyun.com/pypi/simple/ 豆瓣
https://pypi.douban.com/simple/ 百度云
https://mirror.baidu.com/pypi/simple/ 中科大
https://pypi.mirrors.ustc.edu.cn/simple/ 华为云
https://mirrors.huaweicloud.com/repository/pypi/simple/ 腾讯云
https://mirrors.cloud.tencent.com/pypi/simple/

准备工作

首先,确保你已经安装了 openpyxl 库。如果还没有安装,可以使用以下命令进行安装:

pip install openpyxl

步骤 1:导入必要的库

首先,我们需要导入 openpyxl 库中的 load_workbookTranslator 类。

from openpyxl import load_workbook
from openpyxl.formula.translate import Translator

步骤 2:定义填充公式的函数

接下来,我们定义一个名为 fill_down_formulas 的函数。这个函数接受以下参数:

  • filepath:Excel 文件的路径。
  • sheetname:工作表名称。
  • start_row:开始填充公式的行号。
  • start_column:开始填充公式的列号。
  • num_columns:需要填充公式的列数。
def fill_down_formulas(filepath, sheetname, start_row, start_column, num_columns):
try:
# 加载 Excel 文件
wb = load_workbook(filename=filepath)
ws = wb[sheetname]

步骤 3:获取起始单元格的公式

在指定的列范围内,我们首先获取起始单元格的公式。

        # 循环处理每一列
for column_index in range(start_column, start_column + num_columns):
# 获取起始单元格的公式
formula = ws.cell(row=start_row, column=column_index).value
print(f"原始公式 ({start_row}, {column_index}):", formula)

步骤 4:向下填充公式

从起始行的下一行开始,我们将公式填充到该列的其余单元格中。这里使用 Translator 类来更新公式。

            # 从起始行开始填充公式
for row in range(start_row + 1, ws.max_row + 1):
# 获取起始单元格和当前单元格的坐标
start_coordinate = ws.cell(row=start_row, column=column_index).coordinate
current_coordinate = ws.cell(row=row, column=column_index).coordinate
print("起始坐标:", start_coordinate)
print("当前坐标:", current_coordinate) # 使用 Translator 解析并更新公式
translated_formula = Translator(formula, origin=start_coordinate).translate_formula(current_coordinate)
print("翻译后的公式:", translated_formula)
ws.cell(row=row, column=column_index).value = translated_formula

步骤 5:保存修改后的 Excel 文件

填充完公式后,保存修改后的 Excel 文件。

        # 保存修改后的 Excel 文件
wb.save(filepath)
print(f"成功向下填充公式到第 {start_column} 列到第 {start_column + num_columns - 1} 列,起始行 {start_row}")
except Exception as e:
print(f"填充公式时出错: {e}")

步骤 6:执行脚本

在脚本的最后,我们指定 Excel 文件路径、工作表名称、起始行、起始列和列数,并调用 fill_down_formulas 函数。

if __name__ == "__main__":
# 指定 Excel 文件路径、工作表名、起始行、起始列和列数
excel_file_path = "C:\\Users\\Administrator\\Desktop\\销售系数数据同步.xlsx"
sheet_name = "商品费用"
start_row = 2 # 指定起始行
start_column = 47 # 指定起始列
num_columns = 7 # 指定要填充公式的列数 # 调用函数将公式向下填充到指定列和起始行之后
fill_down_formulas(excel_file_path, sheet_name, start_row, start_column, num_columns)

完整代码

from openpyxl import load_workbook
from openpyxl.formula.translate import Translator def fill_down_formulas(filepath, sheetname, start_row, start_column, num_columns):
try:
# 加载 Excel 文件
wb = load_workbook(filename=filepath)
ws = wb[sheetname] # 循环处理每一列
for column_index in range(start_column, start_column + num_columns):
# 获取起始单元格的公式
formula = ws.cell(row=start_row, column=column_index).value
print(f"原始公式 ({start_row}, {column_index}):", formula) # 从起始行开始填充公式
for row in range(start_row + 1, ws.max_row + 1):
# 获取起始单元格和当前单元格的坐标
start_coordinate = ws.cell(row=start_row, column=column_index).coordinate
current_coordinate = ws.cell(row=row, column=column_index).coordinate
print("起始坐标:", start_coordinate)
print("当前坐标:", current_coordinate) # 使用 Translator 解析并更新公式
translated_formula = Translator(formula, origin=start_coordinate).translate_formula(current_coordinate)
print("翻译后的公式:", translated_formula)
ws.cell(row=row, column=column_index).value = translated_formula # 保存修改后的 Excel 文件
wb.save(filepath)
print(f"成功向下填充公式到第 {start_column} 列到第 {start_column + num_columns - 1} 列,起始行 {start_row}")
except Exception as e:
print(f"填充公式时出错: {e}") if __name__ == "__main__":
# 指定 Excel 文件路径、工作表名、起始行、起始列和列数
excel_file_path = "C:\\Users\\Administrator\\Desktop\\销售系数数据同步.xlsx"
sheet_name = "商品费用"
start_row = 2 # 指定起始行
start_column = 47 # 指定起始列
num_columns = 7 # 指定要填充公式的列数 # 调用函数将公式向下填充到指定列和起始行之后
fill_down_formulas(excel_file_path, sheet_name, start_row, start_column, num_columns)

在 Excel 中使用 Python 自动填充公式的更多相关文章

  1. EXCEL 如何实现下拉填充公式,保持公式部分内容不变,使用绝对引用

    EXCEL 如何实现下拉填充公式,保持公式部分内容不变,使用绝对引用 在不想变的单元格前加$符号(列标和列数,两个都要加$),变成绝对引用,默认情况是相对引用 L4固定不变的方式:$L$4 M4固定不 ...

  2. vim中设置Python自动补全

    转自:http://blog.csdn.net/wangzhuo_0717/article/details/6942428 在VIM里面增加python的autocomplete功能的做法如下: 1. ...

  3. [转载]启用 VIM 中的 Python 自动补全及提示功能

    转载: http://zhongwei-leg.iteye.com/blog/941474 周围的同事不喜欢使用 VIM 写 Python 代码的原因之一就是,VIM 不能像 Visual Studi ...

  4. Excel中不常用的一些公式用法

    INDIRECT函数 http://baike.baidu.com/view/3222185.htm 用于使用单元格内容拼凑公式的情况. 1.采用  [工作表名]!单元格名  的形式读取内容: 2.所 ...

  5. excel中如何取消自动超链接?

    最近做的表格有点多,年终述职也到了.总有一些地方生疏了,幸好还有点小印象.记录下来,以后可以回来看看. 方法一 适合单个链接的取消 1 输入网址后,按回车键确认,快捷键ctrl+z,即可取消,这种不好 ...

  6. 关于ThinkPHP中的时间自动填充

    <?php class NewsModel extends Model{ protected $_auto = array( array('time_at','mydate','1','call ...

  7. excel匹配相应条件 自动填充数据

    =VLOOKUP(A6&B6,IF({1,0},Sheet3!$A$3:$A$505&Sheet3!$B$3:$C$505,Sheet3!$Q$3:$Q$505),2,0) =VLOO ...

  8. ThinkPHP中的时间自动填充 无法获取时间

    protected $_auto = array(       array('addTime','time','1','function'),    ); addTime在数据库里的的类型必须为int ...

  9. EXCEL中汉字转大写拼音

    最近一直没有什么成系统的学习东西,也就没写什么随笔.昨天晚上,一哥们儿说给弄个输入汉字直接转拼音的程序,问了他几点需求,说你想做个啥的,最后,他说想做个EXCEL的,现在发现EXCEL确实是个好东西啊 ...

  10. word表格如何实现序号自动填充

    打开word文档,我们需要在如下表格中的准考证号这一列中输入准考证号,手工输入肯定很慢,且容易出错.   我们先选中需要填充准考证号的表格.   选择功能区域中的“开始”,在“段落”组中点击“编号”按 ...

随机推荐

  1. Kafka的实现细节

    Kafka的实现细节 一.Topic和Partition 在Kafka中的每一条消息都有一个topic.一般来说在我们应用中产生不同类型的数据,都可以设置不同的主题.一个主题一般会有多个消息的订阅者, ...

  2. 我们为什么需要操作系统(Operating System)?

    我们为什么需要操作系统(Operating System)? a) 从计算机体系的角度,OS向下统筹了所有硬件资源(1),向上为所有软件提供API调用(2),使得软件程序员不必知晓硬件的具体细节,实现 ...

  3. Oracle使用存储过程实现多行对同行数据排列组合

    Oracle使用存储过程实现同行数据排列组合 对多行的同一行的多列数据进行排列组合 假设获取的原来的数据如下表 A B C D aa ab ac ad 现在我们需要对数据进行处理,将每一行的数据,类似 ...

  4. OpenKruise v1.3:新增自定义 Pod Probe 探针能力与大规模集群性能显著提升

    简介: 在版本 v1.3 中,OpenKruise 提供了新的 CRD 资源 PodProbeMarker,改善了大规模集群的一些性能问题,Advanced DaemonSet 支持镜像预热,以及 C ...

  5. 基于 eBPF 的 Kubernetes 可观测实践

    简介: 阿里云可观测团队构建了 kubernetes 统一监控,无侵入式地提供多语言.应用性能黄金指标,支持多种协议,结合 Kubernetes 管控层与网络系统层监控,提供全栈一体式的可观测体验.通 ...

  6. 全方位事件监控管理,阿里云日志服务Kubernetes事件中心正式上线

    2020年2月21日,阿里云日志服务Kubernetes事件中心正式上线,为Kubernetes事件提供集中化采集.存储.分析.可视化.告警等能力,帮助Kubernetes使用者快速构建准实时.高可靠 ...

  7. Alibaba/IOC-golang 正式开源 ——打造服务于go开发者的IOC框架

    简介: IOC(inversion of control)即控制反转,是面向对象编程中的一种设计原则,可以用来减低计算机代码之间的耦合度.IOC-golang 是一款服务于Go语言开发者的依赖注入框架 ...

  8. OpenKruise v0.8.0 核心能力解读:管理 Sidecar 容器的利器

    简介: OpenKruise 是阿里云开源的云原生应用自动化管理套件,也是当前托管在 Cloud Native Computing Foundation (CNCF) 下的 Sandbox 项目.它来 ...

  9. [PHP] Laravel 联查中对不同表字段关系加条件的方式

    如果条件需要加在 where 条件里,使用 whereColumn,如下示例: whereColumn('A.b_id', '=', 'B.id'); 如果需要加载 join 的 on 之后作为多个条 ...

  10. 修复 VisualStudio 构建时没有将 NuGet 的 PDB 符号文件拷贝到输出文件夹

    本文告诉大家如何修复 VisualStudio 构建时没有将 NuGet 的 PDB 符号文件拷贝到输出文件夹的问题.如果 VisualStudio 构建时没有将 NuGet 的 PDB 符号文件拷贝 ...