如何在Excel中计算公式并使用OpenPyXL提取计算值

时间:2019-06-11 20:41:26

标签: python excel openpyxl

我试图将数量从一种Excel形式复制/粘贴到另一种Excel形式(我用作模板)中,该形式计算COGS,然后取COGS的总和并将其分配给变量。在对OpenPyXL进行了一些研究之后,您似乎无法实时计算公式,因此我正尝试使用复制的数量保存文件,然后重新打开它,获取我的COGS总和,并清除数量以保留它作为空白模板。

但是,当我这样做时,我可以看到它正在粘贴数量值并清除它们,但总COGS值仍为“无”。

这是我在阅读了大部分的《无聊的自动化》之后的第一次尝试Python应用程序,所以我对此很陌生。任何帮助,将不胜感激!

# TODO: Calculate total sell using order form and total cost using ' Costing.xlsx
# Create variables to hold qty of each trim from the total_sheet
costing_wb = openpyxl.load_workbook('Z:\\Costing.xlsx')
costing_sheet = costing_wb['COSTING']
for i in range(3, 46, 1):
    costing_sheet.cell(row=i+2, column=4).value = total_sheet.cell(row=i, column=3).value
# Save Costing Sheet
costing_wb.save('Z:\\Costing.xlsx')

# TODO: total_COGS not grabbing value, returning as 'None'
# Reopen costing sheet and set COGS value
costing_wb = openpyxl.load_workbook('Z:\\Costing.xlsx', data_only=True)
costing_sheet = costing_wb['COSTING']
total_COGS = costing_sheet['I6'].value

# Empty Column D contents and save as blank
costing_wb = openpyxl.load_workbook('Z:\\Costing.xlsx')
costing_sheet = costing_wb['COSTING']
for i in range(5, 48, 1):
    costing_sheet.cell(row=i, column=4).value = None
costing_wb.save('Z:\\Costing.xlsx')

0 个答案:

没有答案