pandas read excel values not formulas

后端 未结 2 1368
挽巷
挽巷 2020-12-17 16:39

Is there a way to have pandas read in only the values from excel and not the formulas? It reads the formulas in as NaN unless I go in and manually save the excel file before

相关标签:
2条回答
  • 2020-12-17 16:51

    I had this problem and I resolve it by moving a graph below the first row I was reading. Looks like the position of the graphs may cause problems.

    0 讨论(0)
  • 2020-12-17 16:54

    That is strange. The normal behaviour of pandas is read values, not formulas. Likely, the problem is in your excel files. Probably your formulas point to other files, or they return a value that pandas sees as nan.

    In the first case, the sheet needs to be updated and there is nothing pandas can do about that (but read on).

    In the second case, you could solve by setting explicit nan values in read_excel:

    pd.read_excel(path, sheetname="Sheet1", na_values = [your na identifiers])
    

    As for the first case, and as a workaround solution to make your work easier, you can automate what you are doing by hand using xlwings:

    import pandas as pd
    import xlwings as xl
    
    def df_from_excel(path):
        app = xl.App(visible=False)
        book = app.books.open(path)
        book.save()
        app.kill()
        return pd.read_excel(path)
    
    df = df_from_excel(path to your file)
    

    If you want to keep those formulas in your excel file just save the file in a different location (book.save(different location)). Then you can get rid of the temporary files with shutil.

    0 讨论(0)
提交回复
热议问题