代码和excel表
excel表:https://wwt.lanzouj.com/iGdMl2136r8d
[Python] 纯文本查看 复制代码import openpyxl
# 此脚本只能读取一行,需解决读取每行问题
def generate_config_files(excel_path):
# 打开Excel工作簿
wb = openpyxl.load_workbook(excel_path)
ws = wb.active
# 获取配置模板
template = []
for row in ws['A']:
if row.value is not None:
template.append(row.value)
# 读取变量名
variable_names = [ws.cell(row=1, column=col).value for col in range(3, 9)]
# 遍历每一组变量值并生成配置文件
variables = {}
for col in range(3, 9): # 从C列到H列
variable_name = ws.cell(row=1, column=col).value.strip('{}')
variable_value = ws.cell(row=2, column=col).value
variables[variable_name] = variable_value
# 生成配置文本
config_text = ""
for row in ws.iter_rows(min_row=1, max_col=26):
line = row[0].value # A列的值
if line is not None:
for var_name, var_value in variables.items():
line = line.replace(f'{{{{{var_name}}}}}', str(var_value))
config_text += line + "\n"
# 使用NAME变量作为文件名
config_filename = f"{variables['NAME']}.cfg" if variables['NAME'] else "unnamed.cfg"
# 写入配置文件
with open(config_filename, 'w') as config_file:
config_file.write(config_text)
# 关闭工作簿
wb.close()
# 替换下面的字符串为您的Excel文件路径
generate_config_files('conf.xlsx')