使用Python打印范围的和值

时间:2016-06-28 18:38:46

标签: python excel openpyxl

我正在尝试在单元格B26中打印该值。到目前为止,我的所有打印输出都是'= SUM(B2:B25)',用于每次迭代。如何打印B2:B25范围内的总和值?

import glob
import openpyxl

path = 'C:/ExcelFolder/*.xlsx'
files = glob.glob(path)
for file in files:
  wb = openpyxl.load_workbook(file)
  sheet2 = wb.get_sheet_by_name('TOTAL')
  Totals = sheet2.cell(row=26, column=2).value
  print(Totals)

输出是:

=SUM(B2:B25)
=SUM(B2:B25)
=SUM(B2:B25)
=SUM(B2:B25)
=SUM(B2:B25)

2 个答案:

答案 0 :(得分:1)

尝试这个

sheet2.cell(row=26, column=2).internal_value


的更新
import glob
import openpyxl

path = 'C:/ExcelFolder/*.xlsx'
files = glob.glob(path)
for file in files:
  wb = openpyxl.load_workbook(file,data_only=True)
  sheet2 = wb.get_sheet_by_name('TOTAL')
  Totals = sheet2.cell(row=26, column=2).value
  print(Totals)

答案 1 :(得分:1)

基于this文档,似乎不是

Totals = sheet2.cell(row=26, column=2).value

你想要像

这样的东西

Totals = sheet2['B26'].value

如果您只想阅读现有工作簿中的该单元格。