使用lxml格式化xml脚本

时间:2018-09-21 21:44:15

标签: python excel xml lxml

我一直在编写一个脚本,该脚本将从excel文件中导出并转换为xml以供我们的服务器加载。该模式正确,除了树中第一个元素“ dataroot”的名称空间外。我相信这正在破坏我们的重要性,并且需要帮助来解决问题。 “ xmlns:od .....”行必须在属性“ generated”之前。任何帮助将非常感激。 谢谢

Xml输出:

<?xml version="1.0" encoding="UTF-8" ?> 
<dataroot generated="2018-09-21T15:23:31" xmlns:od="urn:schemas-microsoft- 
com:officedata">
<TblWorkOrdersExport>
<ID>1</ID> 
<WorkOrderNumber>SU2018tes2</WorkOrderNumber> 
<Requestor>test</Requestor> 
<Agency>Test</Agency> 
<Department>Test</Department> 
<Task_x0020_Code>GIS</Task_x0020_Code> 
<Work_x0020_Scope_x0020_Summary>Sire Import 
Testing</Work_x0020_Scope_x0020_Summary> 
<Address>2001 S</Address> 
<Township_Range>1S2W</Township_Range> 
<Section>21</Section> 
<Assigned_Field>Test</Assigned_Field> 
<Assigned_Office>Test</Assigned_Office> 
<Created_By>Elamunyon</Created_By> 
<Date_Created>2018-09-17 10:33:37</Date_Created> 
<Account_Number>12345</Account_Number> 
<Billing_Status>Test</Billing_Status> 
<WO_Status>IN PROGRESS</WO_Status> 
<Project_Num>Test</Project_Num> 
<Phone_Number>801-403-5683</Phone_Number> 
<Hours_Estimate>4</Hours_Estimate> 
<Cost_Estimate>500</Cost_Estimate> 
<WorkOrderLocation>OFFICE</WorkOrderLocation> 
<Email_Address>test@slco.org</Email_Address> 
</TblWorkOrdersExport>
</dataroot>

Python代码:

import openpyxl
from lxml import etree
import xml.etree.ElementTree as ET
import datetime

#makes a variable that holds the date
now = datetime.datetime.now().strftime("%Y-%m-%d"+"T"+"%H:%M:%S")
#this is the path that needs changed to find the CurrentRecordExportQuery
wb = openpyxl.load_workbook('*path*')
#Reads the 'work book' from an xlsx file
sheet = wb['CurrentRecordExportQuery']
#Parses xlsx data into variables
a=sheet.cell(row=2, column=1).value
b=sheet.cell(row=2, column=2).value
c=sheet.cell(row=2, column=3).value
d=sheet.cell(row=2, column=4).value
e=sheet.cell(row=2, column=5).value
f=sheet.cell(row=2, column=6).value
g=sheet.cell(row=2, column=7).value
h=sheet.cell(row=2, column=8).value
i=sheet.cell(row=2, column=9).value
j=sheet.cell(row=2, column=10).value
k=sheet.cell(row=2, column=11).value
l=sheet.cell(row=2, column=12).value
m=sheet.cell(row=2, column=13).value
n=sheet.cell(row=2, column=14).value
o=sheet.cell(row=2, column=15).value
p=sheet.cell(row=2, column=16).value
q=sheet.cell(row=2, column=17).value
r=sheet.cell(row=2, column=18).value
s=sheet.cell(row=2, column=19).value
t=sheet.cell(row=2, column=20).value
u=sheet.cell(row=2, column=21).value
v=sheet.cell(row=2, column=22).value
#Creates the parent element of the xml tree
root = ET.Element("dataroot")
#if name space needs changed, change the following lines
root.set("xmlns:" + "od", "urn:schemas-microsoft-com:officedata")
#subelement as for the templete that sir wants
doc = ET.SubElement(root, "TblWorkOrdersExport")
#Subelements are added to the previous elements, should match template
ET.SubElement(doc, "ID").text = "1"
ET.SubElement(doc, "WorkOrderNumber").text = a
ET.SubElement(doc, "Requestor").text = b
ET.SubElement(doc, "Agency").text = c
ET.SubElement(doc, "Department").text = d
ET.SubElement(doc, "Task_x0020_Code").text = e
ET.SubElement(doc, "Work_x0020_Scope_x0020_Summary").text = f
ET.SubElement(doc, "Address").text = g
ET.SubElement(doc, "Township_Range").text = h
ET.SubElement(doc, "Section").text = i
ET.SubElement(doc, "Assigned_Field").text = j
ET.SubElement(doc, "Assigned_Office").text = k
ET.SubElement(doc, "Created_By").text = l
ET.SubElement(doc, "Date_Created").text = str(m)
ET.SubElement(doc, "Account_Number").text = str(n)
ET.SubElement(doc, "Billing_Status").text = o
ET.SubElement(doc, "WO_Status").text = p
ET.SubElement(doc, "Project_Num").text = q
ET.SubElement(doc, "Phone_Number").text = r
ET.SubElement(doc, "Hours_Estimate").text = str(s)
ET.SubElement(doc, "Cost_Estimate").text = str(t)
ET.SubElement(doc, "WorkOrderLocation").text = u
ET.SubElement(doc, "Email_Address").text = v
root.set("generated", str(now))
#Makes the xml tree starting at the root variable
tree = ET.ElementTree(root)
#Writes the file to the same folder as the script and adds the encoding
#To change the file location/name and encoding change the line below
tree.write("filename3.xml", encoding="UTF-8", xml_declaration=True)

0 个答案:

没有答案