最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python Excel實(shí)現(xiàn)自動(dòng)添加編號(hào)

 更新時(shí)間:2025年03月13日 15:08:34   作者:一晌小貪歡  
這篇文章主要為大家詳細(xì)介紹了如何使用Python在Excel中實(shí)現(xiàn)自動(dòng)添加編號(hào)效果,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下

1、背景介紹

簡(jiǎn)單的說(shuō),就是在Excel中有一列h=會(huì)有重復(fù)的編號(hào),我們相對(duì)這個(gè)重復(fù)的編號(hào),再次進(jìn)行編號(hào),使其如有重復(fù)就加上-1,-2,-3。。。。。以此類(lèi)推,將重新生成【新編號(hào)】放在其旁邊列

2、庫(kù)的安裝

庫(kù)用途安裝
openpyxlExcel讀取pip install openpyxl -i https://pypi.tuna.tsinghua.edu.cn/simple/
os獲取路徑內(nèi)置庫(kù)無(wú)需安裝

3、核心代碼

①:編號(hào)列的值進(jìn)行計(jì)數(shù)

  • 遍歷Excel文件的每一行,對(duì)編號(hào)列的值進(jìn)行計(jì)數(shù)。
  • 根據(jù)計(jì)數(shù)值生成新的格式化字符串編號(hào)-計(jì)數(shù)值,并更新到結(jié)果編號(hào)列。
  • 支持單個(gè)文件處理和批量文件處理。
for row_idx in range(2, ws.max_row + 1):
    # Get the 序號(hào) value
    序號(hào)_value = ws.cell(row=row_idx, column=序號(hào)_index).value

    # Skip if 序號(hào) is None or empty
    if 序號(hào)_value is None or str(序號(hào)_value).strip() == '':
        continue

    # Convert to string to handle any numeric values
    序號(hào)_str = str(序號(hào)_value)

    # Initialize count if this is the first occurrence
    if 序號(hào)_str not in 序號(hào)_counts:
        序號(hào)_counts[序號(hào)_str] = 0

    # Increment the count
    序號(hào)_counts[序號(hào)_str] += 1

    # Create the new format: "序號(hào)-count"
    new_子賬號(hào)編號(hào) = f"{序號(hào)_str}-{序號(hào)_counts[序號(hào)_str]}"

    # Update the 子賬號(hào)編號(hào) cell
    ws.cell(row=row_idx, column=子賬號(hào)編號(hào)_index).value = new_子賬號(hào)編號(hào)

4、完整代碼

import os
import openpyxl


def process_subaccount_numbers(file_path, output_path=None):
    # Load the workbook
    print(f"Reading file: {file_path}")
    wb = openpyxl.load_workbook(file_path)
    ws = wb.active

    # Get the headers
    headers = [cell.value for cell in ws[1]]

    # Find the indices of the required columns
    if '編號(hào)' not in headers or '結(jié)果編號(hào)' not in headers:
        print("Error: Required columns '序號(hào)' or '結(jié)果編號(hào)' not found in the Excel file.")
        return

    序號(hào)_index = headers.index('編號(hào)') + 1  # +1 because openpyxl is 1-indexed
    子賬號(hào)編號(hào)_index = headers.index('結(jié)果編號(hào)') + 1

    # Create a dictionary to track occurrences of each 序號(hào)
    序號(hào)_counts = {}

    # Process each row (starting from row 2, skipping the header)
    for row_idx in range(2, ws.max_row + 1):
        # Get the 序號(hào) value
        序號(hào)_value = ws.cell(row=row_idx, column=序號(hào)_index).value

        # Skip if 序號(hào) is None or empty
        if 序號(hào)_value is None or str(序號(hào)_value).strip() == '':
            continue

        # Convert to string to handle any numeric values
        序號(hào)_str = str(序號(hào)_value)

        # Initialize count if this is the first occurrence
        if 序號(hào)_str not in 序號(hào)_counts:
            序號(hào)_counts[序號(hào)_str] = 0

        # Increment the count
        序號(hào)_counts[序號(hào)_str] += 1

        # Create the new format: "序號(hào)-count"
        new_子賬號(hào)編號(hào) = f"{序號(hào)_str}-{序號(hào)_counts[序號(hào)_str]}"

        # Update the 子賬號(hào)編號(hào) cell
        ws.cell(row=row_idx, column=子賬號(hào)編號(hào)_index).value = new_子賬號(hào)編號(hào)

    # Determine the output path
    if output_path is None:
        file_name, file_ext = os.path.splitext(file_path)
        output_path = f"{file_name}_processed{file_ext}"

    # Save the modified workbook to a new Excel file
    print(f"Saving processed file to: {output_path}")
    wb.save(output_path)
    print("Processing completed successfully!")

    return output_path


def main():
    # Get the data source directory
    data_dir = './數(shù)據(jù)源/'

    # Find all Excel files in the directory
    excel_files = [f for f in os.listdir(data_dir) if f.endswith('.xlsx') or f.endswith('.xls')]

    if not excel_files:
        print("No Excel files found in the data source directory.")
        return

    # Process each Excel file
    for file_name in excel_files:
        file_path = os.path.join(data_dir, file_name)
        process_subaccount_numbers(file_path)


if __name__ == "__main__":
    main()

效果如下

到此這篇關(guān)于Python Excel實(shí)現(xiàn)自動(dòng)添加編號(hào)的文章就介紹到這了,更多相關(guān)Python Excel添加編號(hào)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

麦盖提县| 南昌县| 彭泽县| 伽师县| 南漳县| 葵青区| 丘北县| 蓝山县| 永州市| 漠河县| 闸北区| 赫章县| 舞钢市| 鞍山市| 乌苏市| 长沙县| 阿鲁科尔沁旗| 昭觉县| 柏乡县| 廉江市| 济宁市| 方山县| 曲阜市| 六枝特区| 儋州市| 康定县| 延安市| 祁连县| 汉阴县| 会理县| 剑阁县| 达孜县| 和政县| 大同市| 北京市| 运城市| 河北区| 饶平县| 安顺市| 福清市| 新巴尔虎右旗|