2020用气量数据汇总文件批量写入公式

1.用到的模块如

openyxl(需要安装)
pip install openyxl
time(python内置模块,不需要安装)

2.代码清单如下:

import openpyxl
#import datetime
import time
# import xlrd
#from xlrd import xldate_as_tuple
# from dateutil.parser import *

# 整点E列填数据=$B$X-DX(X:158,1828)
# (1827-158+1)/5
# i=第1行行号,j=最后一行行号
# k=(j+1-i)/5 总共的数据
#[i,]
# [x * x for x in range(1, 11)]
# [i+5 * x for x in range(0, k)]
# (5 * x + i) for x in range(0, k)
# 修改 i,j 这两个参数即可
i = 158    # i,代表14:00所在行号
j = 1827   # j,代表次日8点所在行号
k = int(( j + 1 - i ) / 5)
# k = 334
row_num = []
print('程序正在处理,请耐心等待。')
for x in range(0, k):
    row_num.append(5 * x + i)

# print(row_num)
# 读取表
book_name = '2020.xlsx'
workbook = openpyxl.load_workbook(book_name)
sheet = workbook['整点']
save_filename = '202001101222.xlsx'

for n in row_num:
    for m in range(0, 5):
        column_n = "={}B{}{}-D{}".format('$','$',n, (n + m) )
        sheet.cell(row = (n + m ), column = 5).value = column_n
    #print(column_n)
    
print('程序处理完成。')
time.sleep(2)
workbook.save(save_filename)
print('文件保存成功!!!')
time.sleep(2)
workbook.close()
print('文件关闭!!!')