简体   繁体   English

我有一个包含文本和计算的 excel 文件,在 python 中读取 excel 文件的最佳方法是什么?

[英]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.我的 excel 文件没有标题或漂亮的行和列。 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? python读取excel文件的最佳方式是什么? I tried openpyxl and can't get pandas to work.我尝试了 openpyxl,但无法让 Pandas 工作。

Sample from excel file:来自excel文件的示例:

    [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.第 1、2、6 和 7 行仅包含文本且位于第一列。 Rows 3 and 8 are texts showing the formulas in columns AE.第 3 行和第 8 行是显示列 AE 中的公式的文本。 Cells A4, B4 and C4 contain data entered.单元格 A4、B4 和 C4 包含输入的数据。 Cells D4, E4, A9, B9 and C9 contains the values after formulas are evaluated.单元格 D4、E4、A9、B9 和 C9 包含计算公式后的值。

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.我需要获取第 4 行和第 9 行中数据的值。对于 Total_Power_Diff,我得到的是公式 =A9/E4 而不是值 0.001332。 For Total_Power_Test, I'm getting None.对于 Total_Power_Test,我得到了 None。 The formula on C9 is =IF(B9 <= 0.05,"PASS","FAIL"). C9 上的公式是 =IF(B9 <= 0.05,"PASS","FAIL")。 I can't figure out how I would use pandas to read this kind of excel file.我不知道如何使用 Pandas 来读取这种 excel 文件。 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. openpyxl 用于读取公式,pandas 会为您提供保存时计算出的 excel 数字。 you will want to use both, use pandas to get numbers, and use openpyxl to edit number and save.您将要同时使用两者,使用 Pandas 获取数字,并使用 openpyxl 编辑数字并保存。 Unless you are only doing read then don't worry about openpyxl.除非你只是在做阅读,否则不要担心 openpyxl。

Now, why pandas doesn't work for you?现在,为什么熊猫不适合你?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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