Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python lxml为ROOT name属性和xml文件提供一个版本_Python_Lxml_Openpyxl - Fatal编程技术网

Python lxml为ROOT name属性和xml文件提供一个版本

Python lxml为ROOT name属性和xml文件提供一个版本,python,lxml,openpyxl,Python,Lxml,Openpyxl,编辑****其他目标: 我希望遍历每个excel行,并将每一行保存为单独的.xml文件filename=invoice.text 谢谢你的帮助 ->>>问题是,第二个创建的.xml文件中也包含来自第一行的数据。有人能帮我吗?高度赞赏 非常感谢您的帮助,我想为根名称属性和xml提供一个版本,并将每个excel行保存为一个单独的.xml文件 我已经用openpyxl设置了excel。 编辑 代码编辑 from lxml import etree import openpyxl # Create

编辑****其他目标: 我希望遍历每个excel行,并将每一行保存为单独的.xml文件filename=invoice.text 谢谢你的帮助 ->>>问题是,第二个创建的.xml文件中也包含来自第一行的数据。有人能帮我吗?高度赞赏

非常感谢您的帮助,我想为根名称属性和xml提供一个版本,并将每个excel行保存为一个单独的.xml文件

我已经用openpyxl设置了excel。

编辑 代码编辑

from lxml import etree
import openpyxl


# Create root element with namespace information
xmlns = "http://xml.datev.de/bedi/tps/ledger/v040"
xsi = "http://www.w3.org/2001/XMLSchema-instance"
schemaLocation = "http://xml.datev.de/bedi/tps/ledger/v040 Belegverwaltung_online_ledger_import_v040.xsd"
version = "4.0"
generator_info = "DATEV Musterdaten"
generating_system = "DATEV manuell"

xmlRoot = etree.Element(
    "{" + xmlns + "}LedgerImport",
    version=version,
    attrib={"{" + xsi + "}schemaLocation": schemaLocation},
    generator_info=generator_info,
    generating_system=generating_system,
    nsmap={'xsi': xsi, None: xmlns}
)

####open excel file speadsheet
wb = openpyxl.load_workbook('import_spendesk_datev.xlsx')
sheet = wb['Import']

# build the xml tree
for i in range(2,6):
        #consolidate = etree.SubElement(xmlRoot, 'consolidate', attrib={'consolidatedAmount': str(sheet.cell(row=i,column=16).value, 'consolidatedDate': str(sheet.cell(row=i,column=2).value, 'consolidatedInvoiceId': str(sheet.cell(row=i,column=13).value, 'consolidatedCurrencyCode': str(sheet.cell(row=i,column=12).value )})
        consolidate = etree.SubElement(xmlRoot, 'consolidate', attrib={'consolidatedAmount': str(sheet.cell(row=i,column=16).value),'consolidatedDate': str(sheet.cell(row=i,column=2).value), 'consolidatedInvoiceId': str(sheet.cell(row=i,column=13).value), 'consolidatedCurrencyCode': str(sheet.cell(row=i,column=12).value) })
        accountsPayableLedger = etree.SubElement(consolidate, 'accountsPayableLedger')
        account = etree.SubElement(accountsPayableLedger, 'bookingText')
        account.text = sheet.cell(row=i,column=21).value
        invoice = etree.SubElement(accountsPayableLedger, 'invoiceId')
        invoice.text = sheet.cell(row=i,column=13).value
        date = etree.SubElement(accountsPayableLedger, 'date')
        date.text = sheet.cell(row=i,column=2).value
        amount = etree.SubElement(accountsPayableLedger, 'amount')
        amount.text = sheet.cell(row=i,column=16).value
        account_no = etree.SubElement(accountsPayableLedger, 'accountNo')
        account_no.text = sheet.cell(row=i,column=19).value
        cost1 = etree.SubElement(accountsPayableLedger, 'costCategoryId')
        cost1.text = sheet.cell(row=i,column=15).value
        currency_code = etree.SubElement(accountsPayableLedger, 'currencyCode')
        currency_code.text = sheet.cell(row=i,column=12).value
        party_id = etree.SubElement(accountsPayableLedger, 'partyId')
        party_id.text = sheet.cell(row=i,column=20).value
        bpaccount = etree.SubElement(accountsPayableLedger, 'bpAccountNo')
        bpaccount.text = sheet.cell(row=i,column=20).value
        #doc = etree.ElementTree(xmlRoot)
        #doc.write( str(sheet.cell(row=i,column=13).value)+".xml", xml_declaration=True, encoding='utf-8', pretty_print=True)
        doc = etree.ElementTree(xmlRoot)
        with open(str(sheet.cell(row=i,column=13).value)+".xml", 'w') as f:
                f.write(etree.tostring(doc, pretty_print=True, xml_declaration=True, encoding='utf-8').decode('utf-8'))



# doc = etree.ElementTree(xmlRoot)
# with open("test1337.xml", 'w') as f:
#     f.write(etree.tostring(doc, pretty_print=True, xml_declaration=True, encoding='utf-8').decode('utf-8'))


# convert into elementtree and write it directly into a file
#doc = etree.ElementTree(xmlRoot)
#outFile = open("test1337.xml", 'w')
#doc.write("test1337.xml", xml_declaration=True, encoding='utf-8', pretty_print=True)
#doc.close()
请不要坐得太久。
非常感谢

我建议使用etree元素和子元素,然后将它们转换为元素树。这在创建xml时提供了更大的灵活性,特别是当您希望迭代现有数据结构时:

from lxml import etree

# Create root element with namespace information
xmlns = "http://xml.datev.de/bedi/tps/ledger/v040"
xsi = "http://www.w3.org/2001/XMLSchema-instance"
schemaLocation = "http://xml.datev.de/bedi/tps/ledger/v040 Belegverwaltung_online_ledger_import_v040.xsd"
version = "4.0"
generator_info = "DATEV Musterdaten"
generating_system = "DATEV manuell"

xmlRoot = etree.Element(
    "{" + xmlns + "}LedgerImport",
    version=version,
    attrib={"{" + xsi + "}schemaLocation": schemaLocation},
    generator_info=generator_info,
    generating_system=generating_system,
    nsmap={'xsi': xsi, None: xmlns}
)

# build the xml tree
consolidate = etree.SubElement(xmlRoot, 'consolidate', attrib={'consolidatedAmount': "1337.01"})
accountsPayableLedger = etree.SubElement(consolidate, 'accountsPayableLedger')
account = etree.SubElement(accountsPayableLedger, 'bookingText')
account.text = 'amazon'
invoice = etree.SubElement(accountsPayableLedger, 'invoiceId')
invoice.text = "1"

# convert into elementtree and write it directly into a file
doc = etree.ElementTree(xmlRoot)
with open("test1337.xml", 'w') as f:
    f.write(etree.tostring(doc, pretty_print=True, xml_declaration=True, encoding='utf-8').decode('utf-8'))
生成的文件如下所示:

<?xml version='1.0' encoding='UTF-8'?>
<LedgerImport xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xml.datev.de/bedi/tps/ledger/v040" generating_system="DATEV manuell" generator_info="DATEV Musterdaten" version="4.0" xsi:schemaLocation="http://xml.datev.de/bedi/tps/ledger/v040 Belegverwaltung_online_ledger_import_v040.xsd">
  <consolidate consolidatedAmount="1337.01">
    <accountsPayableLedger>
      <bookingText>amazon</bookingText>
      <invoiceId>1</invoiceId>
    </accountsPayableLedger>
  </consolidate>
</LedgerImport>

非常感谢你,这很有效,我目前正在做:1。迭代/循环excel电子表格,将每行的值放入一个xml文件中,并保存该文件。希望我能自己弄明白。每个文件的名称必须是Excel中的invoiceId列。很抱歉,此错误显示在终端:回溯最近的调用last:file spendesk_print.py,第40行,在doc.writeoutFile中,xml_declaration=True,encoding='utf-8',pretty_print=True文件src/lxml/etree.pyx,第2056行,在lxml.etree.\u ElementTree.write文件src/lxml/serializer.pxi,第758行,在lxml.etree.\u tofilelike文件src/lxml/etree.pyx,第318行,在lxml.etree.\u ExceptionContext.\u如果将文件src/lxml/serializer.pxi,第682行存储在lxml.etree.\u FilelikeWriter.write类型错误:write参数必须是str,而不是bytesthx,您知道如何将每个excel行保存为一个seprate.xml文件吗在您接受答案后,请不要编辑问题以添加其他目标。好的,我删除。我已经提出了另一个问题