Using Python, I am trying to figure out how to create a XML file based on the input from a CSV. Before a row of data is brought over from the CSV, a section of static information has to be added using the same tags. This could be for 1 row of data or 20 rows. The # of DocFile tags should be double the amount of rows.
<DocFile>
<FullPathName>P:\StaticFile.pdf</FullPathName>
<PageRange UseAllPages="true"/>
<Quantity>1</Quantity>
<Section>1</Section>
</DocFile>
<DocFile>
<FullPathName>row[0]</FullPathName>
<PageRange UseAllPages="true"/>
<Quantity>row[1]</Quantity>
<Section>1</Section>
</DocFile>
This is a sample of 1 row of data.
Below is what I have written so far. The problem that I am having is that only one section of DocFile is making it to the XML. FYI I am relatively new to Python.
`import csv
from lxml import etree as ET
# Assign file locations
csvFile = '/Users/jehringer/python_work/Work Testing/CSV/Order_123.csv'
xmlFile = '/Users/jehringer/python_work/Work Testing/CSV/myXMLFile.xml'
# Build XML Structure
root = ET.Element('UltimateImposition')
redirect = ET.SubElement(root, 'Redirection')
printJob = ET.SubElement(redirect, 'PrintJob')
queue = ET.SubElement(printJob, 'QueueName')
documents = ET.SubElement(printJob, "Documents")
docFile = ET.SubElement(documents, "DocFile")
fullName = ET.SubElement(docFile, "FullPathName")
pageCount = ET.SubElement(docFile, "PageRange")
qty = ET.SubElement(docFile, "Quantity")
section = ET.SubElement(docFile, "Section")
# Define Static Values
ET.SubElement(queue, "Name").text = "process"
ET.SubElement(queue, "FullPathName").text = r"P:\process"
# Open CSV and run through
with open(csvFile, 'r') as csvFiles:
csvData = csv.reader(csvFiles, delimiter=',')
for row in csvData:
for i in range(2):
if i == 1:
fullName.text = "2up_v4_ejectjob.pdf"
pageCount.set("UseAllPages", "true")
qty.text = "1"
section.text = '1'
else:
fullName.text = row[0]
pageCount.set("UseAllPages", "true")
qty.text = row[1]
section.text = '1'
# Write to XML
tree = ET.ElementTree(root)
tree.write(xmlFile, pretty_print=True, xml_declaration=True)`