简体   繁体   中英

I have an excel file that has texts and calculations, what's the best way to read the excel file in python?

My excel file doesn't have headers or nice rows and columns. It also has formulas. I want to read the values of the formula calculated, not the formula. What's the best way for python to read the excel file? I tried openpyxl and can't get pandas to work.

Sample from excel file:

    [A]        [B]        [C]             [D]                [E]
[1] Test Case 3 - Step 8                    
[2] Enter the PL, PF, and PR values from the VSWR file below, the formula will calculate the P1 and P2 values that can be entered in the TPR.                   
[3] PL         PF         PR              P1 = (VL*IL)/2     P2 = PL = PF-PR    
[4] 39.3470    39.4508    0.1038          39.3994            39.3470    
[5]                     
[6] Test Case 3 - Step 9                    
[7] The formula will compare the P1 and P2 values calculate above and determine if they are within the tolerance of 10%.                    
[8] |P1-P2|    % Diff     Tolerance 5.0%            
[9] 0.0524     0.1332%    PASS          

Rows 1, 2, 6, and 7 only contain texts and are on column one. Rows 3 and 8 are texts showing the formulas in columns AE. Cells A4, B4 and C4 contain data entered. Cells D4, E4, A9, B9 and C9 contains the values after formulas are evaluated.

Code:

import openpyxl
from openpyxl import load_workbook

wbt = load_workbook(filename = "Power_Assurance_Results.xlsx")
wst = wbt.active
Total_Power_Diff = wst['B9'].value
Total_Power_Test = wst['B28'].value

I need to get the values of the data in rows 4 and 9. For Total_Power_Diff, I'm getting the formula =A9/E4 and not the value 0.001332. For Total_Power_Test, I'm getting None. The formula on C9 is =IF(B9 <= 0.05,"PASS","FAIL"). I can't figure out how I would use pandas to read this kind of excel file. I thought I read that I can get the evaluated value from the cell and not the formula.

openpyxl is for reading formula, and pandas would get you the numbers excel calculated at save. you will want to use both, use pandas to get numbers, and use openpyxl to edit number and save. Unless you are only doing read then don't worry about openpyxl.

Now, why pandas doesn't work for you?

import pandas as pd
wbt = pd.read_excel('Power_Assurance_Results.xlsx')
wbt.head()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM