我想将excel中的数据自动填入一个字符串并顺序输出
问题相关代码
from docxtpl import DocxTemplate
from openpyxl import load_workbook
import os
def replace(obj):
if obj is None:
obj = ''
return obj
wb = load_workbook("excel.xlsx")
'''
这是excel.xlsx中的内容
AID BID CID
123 2022-07 e:147
456 2022-09 g:258
'''
ws = wb['Sheet1']
contexts = []
for row in range(2, ws.max_row + 1):
a_id = ws["A" + str(row)].value
b_id = ws["B" + str(row)].value
c_id = ws["C" + str(row)].value
context="BId \"{{b_id}}\" -CId \"{{c_id}}\" -APath \"D:\\{{a_id}}.txt\""
contexts.append(context)
contexts
for context in contexts:
print(context)
运行结果及报错内容
BId "{{B_id}}" -CId "{{c_id}}" -APath "D:{{a_id}}.txt"
BId "{{B_id}}" -CId "{{c_id}}" -APath "D:{{a_id}}.txt"
我想要达到的结果
输出以下结果
BId "2022-07" -CId "e:147" -APath "D:\123.txt"
BId "2022-09" -CId "g:258" -APath "D:\456.txt"