0

I am trying to write to an excel file and then load the result into a DF. However, calculated values are returning N/A in the DF even though when I open the excel sheet they are correctly displayed.

If I open and then save the excel sheet manually after updating using python, loading the dataframe works.

Here is the code:

from openpyxl import load_workbook
import pandas as pd

if __name__ == '__main__':
    portfolio_values = getBalances('USD')
    wb = load_workbook(filename = 'Client_Portfolio_Tracker.xlsx')

    clients = wb['Clients']
    clients['F2'] = portfolio_values
    clients['G2'] = datetime.datetime.now().strftime("%m/%d/%y %H:%M")

    wb.save('Client_Portfolio_Tracker.xlsx')
    client_df = pd.read_excel('Client_Portfolio_Tracker.xlsx', sheetname = 'Clients')
    print(client_df)

Output of dataframe

Thanks in advance!

  • try explicitly closing the workbook or using a `with` block – Paul H Jan 27 '18 at 00:44
  • Can you expand on a "with block?" Sorry, I am new to this. Closing was unsuccessful unfortunately – deltaneutral Jan 27 '18 at 00:46
  • Like this question, but with `load_workbook` instead of `open`. https://stackoverflow.com/questions/9282967/how-to-open-a-file-using-the-open-with-statement – Paul H Jan 27 '18 at 00:49
  • Gives me an "AttributeError: __exit__" and when I look it up seems that I should just be setting it directly to a variable. Thank you for your help though, appreciate you taking the time – deltaneutral Jan 27 '18 at 00:55

0 Answers0