我有一个场景,我需要从 XML 文件中获取数据并将其写入 Excel 工作表并使用同一个工作表进行数据处理。我能够从 XML 读取数据,但无法将相同的数据(记录)插入到我正在使用 OpenPyExcel 的 excel 文件中,请提出任何替代方案并在此处帮助我。虽然我没有看到任何错误,但没有任何内容被写入 excel 表
import xml.etree.ElementTree as ET
import openpyexcel
tree = ET.parse("Test_Cust.xml")
root = tree.getroot()
workbook = openpyexcel.load_workbook("xml_excel.xlsx")
sheet = workbook["Sheet1"]
for items in root.iter():
if items.tag == "Email":
cust_email = items.text
elif items.tag == "CompanyName":
cust_cn = items.text
elif items.tag == "FirstName":
cust_fn = items.text
elif items.tag == "LastName":
cust_ln = items.text
rownum = (sheet.max_row)
print(rownum)
colnum = (sheet.max_column)
print(colnum)
for r in range(2, rownum+1):
for c in range(1, colnum+1):
sheet.cell(row = r, column = c).value = cust_email
sheet.cell(row=r, column=c).value = cust_email
sheet.cell(row=r, column=c).value = cust_email
sheet.cell(row=r, column=c).value = cust_email
workbook.save("xml_excel.xlsx")
print("Done")