Python - Openpyxl - 如果 A 列(列表)和元组(列表)之间匹配,则将单元格值添加到 P 列

Python - Openpyxl - Adding Cell Value To Column P If Match Between Column A (list) and Tuple (list)

背景

我在 A 列中有一个值列表(每个工作表都不同)。我想确定要匹配的值的子集,如果在列表之间进行了匹配,那么脚本会将元组列表中的值添加到 P 列并按值向下移动(参见图片脚本输出)。

问题

脚本的工作原理,如果在 A 列列表中找不到元组中的值,则接受,然后它在 Excel 中创建一个空单元格并向下移动到下一个单元格(见图 - Excel 输出).

问题

  1. 如果元组中的值不在列表中,避免空单元格的最佳方法是什么?

脚本

from openpyxl import Workbook
from openpyxl import load_workbook
from openpyxl.styles import Color, PatternFill, Font, Border
from openpyxl.styles import colors
from openpyxl.cell import Cell
import colorama
from colorama import init
init()
from colorama import Fore, Style, Back
import os
import shutil
import math
from decimal import Decimal
from openpyxl.styles import Alignment
from inspect import currentframe
import time

########################################
#########################################

src_path ="/Users/data/"
xlfileName = "test.xlsx"
xlfile = src_path + xlfileName
wb = Workbook()
wb = load_workbook(xlfile)

#########################################
#########################################

def get_linenumber():
    cf = currentframe()
    return cf.f_back.f_lineno

#########################################
#########################################
non_cash_tup = (
"Depreciation & amortisation", "Deferred income taxes", "Stock-based compensation", "Change in working capital",
"Accounts receivable", "Inventory", "Accounts payable", "Other non-cash items")

for xws in wb.sheetnames:
    print(Style.RESET_ALL)
    print(Fore.BLUE, "Looping Through Worksheet Name -- ", worksheet, "Script line is -- ", get_linenumber())
    print(Style.RESET_ALL)
    worksheet = wb[xws]
    cell_list = []

    for cell in worksheet['A']:
        cell_list.append(cell.value)
    for i, name in enumerate(non_cash_tup):
        if name in cell_list:
            print(Fore.LIGHTGREEN_EX, name, "is in the list")
            column_cell = 16
            worksheet.cell(column=column_cell, row=i+2, value=name)
        else:
            print(Fore.LIGHTRED_EX, name, "is not in the list")
    wb.save(xlfile)

使用set()函数。

Python 中的 set() 函数用于获取参数并将其转换为集合对象。它可以接受列表、元组和字典等参数。该参数称为可迭代的。元素的输出顺序可能不同,因为作为列表传递的项目顺序不对。

non_cash_tup = (
"Depreciation & amortisation", "Deferred income taxes", "Stock-based compensation", "Change in working capital",
"Accounts receivable", "Inventory", "Accounts payable", "Other non-cash items")

for xws in wb.sheetnames:
    print(Style.RESET_ALL)
    print(Fore.BLUE, "Looping Through Worksheet Name -- ", worksheet, "Script line is -- ", get_linenumber())
    print(Style.RESET_ALL)
    worksheet = wb[xws]
    cell_list = []

    for cell in worksheet['A']:
        cell_list.append(cell.value)

    matching = set(non_cash_tup) & set(cell_list)
    #print(matching)
    for i, name in enumerate(matching):
        print(Fore.LIGHTGREEN_EX, name, "is in the list")
        column_cell = 16
        worksheet.cell(column=column_cell, row=i+2, value=name)
        wb.save(xlfile)