2

I am trying to print the value in the cell B26. So far all my print out is '=SUM(B2:B25)' for each iteration. How can I print the sum value in the range of 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)

Output is:

=SUM(B2:B25)
=SUM(B2:B25)
=SUM(B2:B25)
=SUM(B2:B25)
=SUM(B2:B25)
wisenhiemer
  • 155
  • 2
  • 5
  • 14

2 Answers2

1

try this one

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

update

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)
galaxyan
  • 5,944
  • 2
  • 19
  • 43
1

Based on this documentation, it seems like instead of

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

You'd want something like

Totals = sheet2['B26'].value

if you just want to read at that cell in an existing workbook.

Ryan
  • 387
  • 3
  • 13