Openpyxl finds the corresponding numeric column number and letter column number according to the column name

First import the required packages

from openpyxl.utils import get_column_letter, column_index_from_string

Find the numeric column number based on the column name

Determine the value by iterating over the first row

title_list = ['id', 'sku链接','图片链接', '图片', '价格', '规格', '活动']
# 列名为以上列表

for ever_col in range(1, ws.max_column + 1):
    if ws.cell(1, ever_col).value == "图片链接":
       img_col = ever_col
        print(img_col)
        break
# 3

Find alphabetical column numbers based on column names

title_list is the first row of data, which is the title row

Find the index corresponding to the list where [Image Link] is located

The list subscript starts from 0, and the workbook subscript starts from 1, so you need to add 1

from openpyxl.utils import get_column_letter, column_index_from_string

title_list = ['id', 'sku链接','图片链接', '图片', '价格', '规格', '活动']

img_index = title_list.index('图片链接') 
# img_index = 2

letter_index = get_column_letter(img_index + 1)

print(letter_index )
# C

 

Guess you like

Origin blog.csdn.net/gongzairen/article/details/131068161