简体   繁体   English

如何使用Python将XML文件导入Excel XLS文件模板?

[英]How to import an XML file into an Excel XLS file template using Python?

I have an Excel template file which has columns as defined within an XML. 我有一个Excel模板文件,其中包含XML定义的列。 Manually I can right click over the template then XML > import, and select the XML file, and finally save the file. 我可以手动右键单击模板,然后单击XML>导入,然后选择XML文件,最后保存该文件。

How can I perform this task automatically programming it in Python ? 我如何执行此任务,然后在Python中自动对其进行编程

Example of XML file: XML文件示例:

<DCPowerFlow>
    <branches>
        <branch>
            <busFrom name="bus_one" number="1" />
            <busTo name="bus_two" number="2" />
            <id>1</id>
            <rateA>1000</rateA>
            <resultPowerFlow>
                <probOverFlow>0.0</probOverFlow>
                <maxOverFlow>800</maxOverFlow>
            </resultPowerFlow>
        </branch>
        <branch>
            <busFrom name="bus_two" number="2" />
            <busTo name="bus_three" number="3" />
            <id>1</id>
            <rateA>1200</rateA>
            <resultPowerFlow>
                <probOverFlow>0.1</probOverFlow>
                <maxOverFlow>1300</maxOverFlow>
            </resultPowerFlow>
        </branch>
    </branches>
</DCPowerFlow>

To check for the manual task: 要检查手动任务:

  1. Save the above example as an XML file. 将以上示例另存为XML文件。
  2. In order to create the Excel XLS template you can simply open the above XML example with Excel, ensure no data is on the template (delete data if within the import you added any) and save the file as XLS. 为了创建Excel XLS模板,您可以简单地使用Excel打开上述XML示例,确保模板上没有数据(如果在导入中添加了任何数据,则删除数据)并将文件另存为XLS。
  3. Import the example XML file. 导入示例XML文件。 Right click on the Excel template file created, then XML > Import, and select the XML example file. 右键单击创建的Excel模板文件,然后单击XML>导入,然后选择XML示例文件。
  4. Save Template with data as a new XLS. 将包含数据的模板另存为新的XLS。

So what I need to do is to automate steps 3 and 4 . 因此,我需要做的是使步骤3和4自动化

Have you tried using BeautifulSoup and Pandas? 您是否尝试过使用BeautifulSoup和Pandas? Note that the parser I use in the following script requires you to have lxml installed already. 请注意,我在以下脚本中使用的解析器要求您已经安装了lxml。 If you don't have it just pip install lxml. 如果没有,只需pip安装lxml。

import pandas as pd
from bs4 import BeautifulSoup

file = open("file.xml", 'r')
soup = BeautifulSoup(file, 'lxml')
df = pd.DataFrame({'ids': [x.text for x in soup.find_all('id')]})
df.to_excel('data.xls')

While you will have to figure out how you want to parse your file, that will give you the tools that you need. 尽管您将必须弄清楚如何解析文件,但是这将为您提供所需的工具。 If you need more information about how to parse the file, try visiting the BeautifulSoup documentation . 如果您需要有关如何解析文件的更多信息,请尝试访问BeautifulSoup文档 Using this code you can loop through all the files that you are interested in and parsing them into dataframes, then exporting them using the to_excel method. 使用此代码,您可以循环浏览所有感兴趣的文件,并将其解析为数据帧,然后使用to_excel方法将其导出。

Finally I could figure this out using win32com.client module. 最后,我可以使用win32com.client模块弄清楚这一点。

I used the following code successfully to import an xml to an existing Excel xlsx file I use as template, and then save it with a different name: 我成功使用以下代码将xml导入到我用作模板的现有Excel xlsx文件中,然后将其保存为其他名称:

import win32com.client as win32
excel = win32.gencache.EnsureDispatch('Excel.Application')
wb = excel.Workbooks.Open("D:/tmp/template.xlsx")
wb.XmlImport("D:/tmp/result.xml")
wb.SaveAs("D:\\tmp\\result.xlsx")
wb.Close()

Methods for Excel workbooks can be found here . Excel工作簿的方法可以在这里找到。 Also I had to take into account that the saveAs method doesn't support forward slashes . 另外我还必须考虑到saveAs方法不支持正斜杠

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM