You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
from openpyxl import load_workbook
def column_letter_to_number ( column_letter ) :
"""
将 Excel 列字母转换为数字
:param column_letter: 列字母,如 " A " , " B " , " Z " , " AA " , " AB " 等
:return: 列数字, 例如 " A " 返回 1, " B " 返回 2
"""
result = 0
for i , letter in enumerate ( reversed ( column_letter ) ) :
result + = ( ord ( letter ) - ord ( ' A ' ) + 1 ) * ( 26 * * i )
return result
# 输入文件路径
xlsx_file = r ' D: \ dsWork \ 2025年收集的人口与教育数据库( 20250328) \ 2015-2020年的数据 \ 基础教育 \ 2019.xlsx '
sheet_name = ' 基教小学 '
# 从第 8 行开始是有效数据
row_start = 8
# 定义列索引(列索引从 1 开始)
col_school_name = 2 # B 列:学校名称
col_school_type = 4 # D 列:办学类型
col_school_count = 6 # F 列:学校数量
# 加载 Excel 文件
wb = load_workbook ( xlsx_file )
ws = wb [ sheet_name ]
col_city = column_letter_to_number ( ' MM4 ' ) # MM4 列:市
col_district = column_letter_to_number ( ' MN4 ' ) # MN4 列:区
# 遍历有效数据行并输出指定列的值
# for row in ws.iter_rows(min_row=row_start, values_only=True):
# school_name = row[col_school_name - 1] # B 列:学校名称
# school_type = row[col_school_type - 1] # D 列:办学类型
# school_count = row[col_school_count - 1] # F 列:学校数量
# city = row[col_city - 1] # MM 列:市
# district = row[col_district - 1] # MN 列:区
#
# if school_count > 0:
# # 打印指定列的值
# print(f"学校名称: {school_name}, 办学类型: {school_type}, 学校数量: {school_count}, 市: {city}, 区: {district}")
# 关闭 Excel 文件
ws . close ( )
wb . close ( )