Search code examples
pythonopenpyxlworksheet

How come cell value is not being printed/modified using openpyxl?


So, I have the following code which is supposed to create a new column with the corrected prices and then a Bar Chart, however, the code creates an empty chart and also does not create a new column with new prices.

import openpyxl as xl
from openpyxl.chart import BarChart, Reference

def process_workbook(filename):
    wb = xl.load_workbook(filename)
    sheet = wb['Sheet1']

    for row in range(2, sheet.max_row + 1):
        cell = sheet.cell(row, 3)
        corrected_price = cell.value * 0.9
        corrected_price_cell = sheet.cell(row, 4)
        corrected_price_cell = corrected_price

    values = Reference(sheet,
              min_row=2,
              max_row=sheet.max_row,
              min_col=4,
              max_col=4)

    chart = BarChart()
    chart.add_data(values)
    sheet.add_chart(chart, 'e2')

    wb.save(filename)

This is what the xl file looks like, and it's called transactions.xlsx, but it is never modified. So, I really don't know what I am doing wrong.

I did not try anything else besides what's shown up there.


Solution

  • The excel sheet provided shows data in cells A1:C4 including the headers.
    In your code there is

    corrected_price_cell = sheet.cell(row, 4)
    

    which references column D where there is no data but since that assignment is to the cell (not the value) it is a cell object anyway. In the next line you assign that same variable the value of 'corrected prices' so maybe that first line is in error. But then corrected_price_cell is never used in your code.

    for row in range(2, sheet.max_row + 1):
        cell = sheet.cell(row, 3)
        corrected_price = cell.value * 0.9
        corrected_price_cell = sheet.cell(row, 4)
        corrected_price_cell = corrected_price
    

    After that you assign your values range for the chart as D2:D4, a range with no data.

    values = Reference(sheet,
              min_row=2,
              max_row=sheet.max_row,
              min_col=4,
              max_col=4)
    

    so the resulting chart is blank.
    Your new column creation is not working

    To create that column you want to do something like this;

    def process_workbook(filename):
        wb = xl.load_workbook(filename)
        sheet = wb['Sheet1']
    
        for row in sheet.iter_rows(min_col=3, max_col=3):
            ### Add Header
            if row[0].row == 1:
                sheet.cell(row=row[0].row, column=4).value = 'Calculated'
                sheet.cell(row=row[0].row, column=4).font = Font(bold=True)
            else:
                ### Add calculated value
                sheet.cell(row=row[0].row, column=4).value = row[0].value * 0.9
                ### Set cell number format to currency
                sheet.cell(row=row[0].row, column=4).number_format = '"$"#,##0.00'
    
        values = Reference(sheet,
                  min_row=2,
                  max_row=sheet.max_row,
                  min_col=4,
                  max_col=4)
    
        chart = BarChart()
        chart.add_data(values)
        sheet.add_chart(chart, 'E2')
    
        wb.save(filename)