如何使用python读取Excel合并的单元属性值?

问题描述 投票:0回答:1
我需要从Excel文件读取数据。第一个单元格包含属性名称,第二个单元格包含属性值。但是,第一列中的某些属性名称在两个或多个列中合并,相应的值在下一个单元格中。例如,属性名称“ ref”在A和B列中,其值在C列中。

我想从我的Excel文件中的C列检索“ REF”属性的值。

这里是我的excel图像:

我正在使用Python。这是输出:enter image description here Approval Memo of : SHILPI AKTER Name of the Applicant : SHILPI AKTER Name of Territory : Comilla Total Family Expenses : 30000 Ref : N/A Amount : N/A Total Amount : 3000

找不到REF和MANTER

属性值。 这是我的代码:

     import os
    import openpyxl
    from openpyxl.utils import column_index_from_string

    file_path = r"D:\file\input\example.xlsx"  
    if os.path.exists(file_path):
        print("File exists!")
    else:
        print("File not found! Check the path.")
        exit()

    target_sheet = "Output Approval Templete"

    # Define the properties to extract
    properties = [ 
        "Approval Memo of",
        "Name of the Applicant",
        "Name of Territory",
        "Total Family Expenses",
        "Ref",
        "Amount",
        "Total Amount"
    ]

    # Function to get the actual value from a merged cell
    def get_merged_cell_value(sheet, row, col):
        for merged_range in sheet.merged_cells.ranges:
            min_row, min_col, max_row, max_col = merged_range.bounds  # Extract merged cell bounds
            if min_row <= row <= max_row and min_col <= col <= max_col:
                return sheet.cell(min_row, min_col).value  # Return the first cell's value of the merged range
        return sheet.cell(row, col).value  

    # Function to format numeric values properly
    def format_value(value):
        if isinstance(value, float) and value > 1e10:  # Large numbers like NID
            return str(int(value))  # Convert to integer and string to avoid scientific notation
        elif isinstance(value, (int, float)):  # General number formatting
            return str(value)
        elif value is None:
            return "N/A"  # Handle missing values
        return str(value).strip()

    try:
        # Load the workbook
        wb = openpyxl.load_workbook(file_path, data_only=True)

        if target_sheet not in wb.sheetnames:
            print(f"Sheet '{target_sheet}' not found in the file.")
        else:
            ws = wb[target_sheet]
            extracted_data = {}

            # Iterate over rows to extract data
            for row in ws.iter_rows():
                for cell in row:
                    # Check if the cell value is a property we are looking for
                    if cell.value and isinstance(cell.value, str) and cell.value.strip() in properties:
                        prop_name = cell.value.strip()
                        col_idx = cell.column  # Get column index (1-based)
                        next_col_idx = col_idx + 1  # Next column index

                        # Ensure next column exists within sheet bounds
                        if next_col_idx <= ws.max_column:
                            # Check if the cell is merged, and get its value
                            next_value = get_merged_cell_value(ws, cell.row, next_col_idx)
                            
                            # Store the formatted value for the property
                            extracted_data[prop_name] = format_value(next_value)  # Store extracted value

            # Print extracted values
            for key, value in extracted_data.items():
                print(f"{key} : {value}")

    except Exception as e:
        print(f"Error loading workbook: {e}")
plaplease帮助我找出合并属性值。

仅在范围列号中获取最后一个单元格,并像您在其他字段中一样添加1个单元格。

该代码假定合并单元格仅行。 还假设关键名称是单元格与属性列表中的名称完全相同
import os
import openpyxl


def get_next_cell(lc):
    for merge in ws.merged_cells:
        if lc in merge.coord:
            print(merge.coord)
            return merge.top[-1][1]+1


def format_value(value):
    if isinstance(value, float) and value > 1e10:  # Large numbers like NID
        return str(int(value))  # Convert to integer and string to avoid scientific notation
    elif isinstance(value, (int, float)):  # General number formatting
        return str(value)
    elif value is None:
        return "N/A"  # Handle missing values
    return str(value).strip()


# Define the properties to extract
properties = [
    "Approval Memo of",
    "Name of the Applicant",
    "Name of Territory",
    "Total Family Expenses",
    "Ref",
    "Amount",
    "Total Amount"
]

# Init Dictionary 
extracted_data = {}  

# Set working sheet name
target_sheet = "Output Approval Templete"


# Load the workbook
file_path = r"D:\file\input\example.xlsx"

if os.path.exists(file_path):
    print("File exists!\n")
else:
    print("File not found! Check the path.")
    exit()

wb = openpyxl.load_workbook(file_path, data_only=True)
ws = wb.active

# Check working sheet exists
if target_sheet not in wb.sheetnames:
    print(f"Sheet '{target_sheet}' not found in the file.")
else:
    ws = wb[target_sheet]

# Process rows 
for row in ws.iter_rows(1):
    for cell in row:
        col = 1
        cv = cell.value
        if isinstance(cell.value, str):   # Strip white spaces if the cell value is a string
            cv = cv.strip()
        if cv in properties:  # Process only cells with value in the 'properteries' List
            co = cell.coordinate
            print(f"Processing '{cv}' in 'Properties' List at cell {co}")

            if co in ws.merged_cells:  # Check if the current cell is in a merge
                print('This is also a merged cell:')
                col = get_next_cell(co)   # If merged get the next col number after the merge range
            else:
                col = cell.col_idx + 1    # If not merged get the next col number after the cell

            next_value = ws.cell(cell.row, col).value  # Get next cell value as determined by value of 'col'
            print(f"Inserting Key: '{cv}' with Value: {next_value}")
            extracted_data[cv] = format_value(next_value)  # Add key and value to the dictionary
            print("-----------\n")

for key, value in extracted_data.items():
    print(f"{key} : {value}")

python excel pandas openpyxl
1个回答
0
投票

用稍微修改的Excel表进行测试




Approval Memo of : SHILPI AKTER Name of the Applicant : SHILPI AKTER Name of Territory : Comilla Total Family Expenses : 30000 Ref : 22000 Amount : 5000 Total Amount : 3000


最新问题
© www.soinside.com 2019 - 2025. All rights reserved.