Files
YunNanProject/Tools/T2_GetJson.py
2025-09-10 10:09:31 +08:00

41 lines
1.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 读取 Doc目录 数据库-2015-2024-v2.xlsx 文件内容
# pip install openpyxl
import openpyxl
from Config.Config import EXCEL_PATH
from Util.AreaUtil import query_area_info
file_name = EXCEL_PATH
# 读取Excel文件并获取所有sheet名称
try:
# 加载工作簿
workbook = openpyxl.load_workbook(file_name, read_only=True)
sheet_names = workbook.sheetnames
if sheet_names:
first_sheet = workbook[sheet_names[0]]
# 读取所有行数据
rows = list(first_sheet.iter_rows(values_only=True))
# 跳过前两行表头,从第三行开始处理数据
for row_num, row in enumerate(rows[2:], start=3):
# 假设行政区划名称在第一列索引0
if row and row[0]:
area_name = row[0]
result = query_area_info(area_name)
if result:
print(
f"{row_num}: {area_name} -> 全称: {result['full_name']}, 行政区划码: {result['area_code']}")
else:
print(f"{row_num}: 未找到 '{area_name}' 的相关信息")
workbook.close()
except FileNotFoundError:
print(f"错误:找不到文件 '{file_name}'")
except Exception as e:
print(f"读取Excel时发生错误{str(e)}")