I have a Python source file which I would like to convert, according to a certain template written in Apache Velocity, to an XML file.
Is it possible to read the contents of a python code file and extract the necessary information in native Velocity language? Or do I need to write a python script (or some other language)to parse the python file to the xml template?
As you have identified, you need to have two distinct phases:
Extract information from the Python source file (aka parsing)
Publish this information back to an XML file
Apache Velocity can help you accomplish step 1, but knows nothing about parsing.
There are several ways to achieve the parsing step:
you could parse it with whatever tool you like, and publish it in a format easily understandable for Velocity, like a Properties file that you would put in the context. If you need to use hierarchical properties, have a look at the ValueParser tool, which can return submaps from sets of properties like foo.bar=woogie and foo.schmoo=wiggie.
you could give a try to Stillness, a parsing tool that uses a Velocity-like syntax (disclaimer: I wrote it).
Related
I am new with python and need to implement an interface to an accounting tool. I received some XSD Files which describes the interface.
What is the easiest way to generate the XML according to the XSD?
Is there any module I can use?
Do I have to create the XML all by myself and I can use the XSD just to verify it?
How do I best proceed?
I think, generateDS is the solution to your problem.
Starting from chapter 5, the command
python generateDS.py -o people.py -s peoplesubs.py people.xsd
reads the XSD file and creates several classes and subclasses. It generates many data structures and getters and setters for accessing and using data :)
If there is any XML file that complies with that XSD, it can be read straight away by using
import people
rootObject = people.parse('people.xml')
within the code. More information is given in chapter 12.
The aforementioned classes also provide methods to export data as an XML format.
The level of documentation is good and it is highly suggested to use this for any future project.
There are some projects on github that do that by using xmlschema library, for instance fortesp/xsd2xml or miaozn/xsd2xml (python2)
For instance with the former:
xmlgenerator = XMLGenerator('resources/pain.001.001.09.xsd', True, DataFacet())
print(xmlgenerator.execute()) # Output to console
xmlgenerator.write('filename.xml') # Output to file
Unfortunately none of these are properly packaged though.
I would like to produce some custom output with python with data from Tableau files. I dont have access to the Tableau server to run the 'Tabpy' library.
Is there any other way to do it?
Thank you in advance
You may find the following link useful.
https://community.tableau.com/thread/152463
One of the posts in the thread mentioned the following which is worth exploring:
If you're looking to generate a TWBX dynamically, you should rename
your .twbx file to .zip, extract the contents and you can do whatever
you want with those in Python to dynamically create or adjust a
workbook file. The structure / definition of the workbook file is just
XML so no special code needed to read and parse that.
I'm finding resources to do either importing xml from a file or escaping html in python (so that the stuff like & ampersands are imported/converted properly and don't cause errors) but what I'm finding is not recent and/or doesn't really use methods that put both tasks together at the same time.
The task is to take an xml file generated from the amazon api, which always has unescaped ampersands in the output (I have to create a file because amazon has to approve the code before they allow me to make api requests from the program itself) and then parse through it, eventually providing me with an object I can get data from.
How would you do this in 2017? Easy answers definitely appreciated, I'm not exactly a pro at python coding yet.
I want to enter data into a Microsoft Excel Spreadsheet, and for that data to interact and write itself to other documents and webforms.
With success, I am pulling data from an Excel spreadsheet using xlwings. Right now, I’m stuck working with .docx files. The goal here is to write the Excel data into specific parts of a Microsoft Word .docx file template and create a new file.
My specific question is:
Can you modify just a text string(s) in a word/document.xml file and still maintain the integrity and functionality of its .docx encasement? It seems that there are numerous things that can change in the XML code when making even the slightest change to a Word document. I've been working with python-docx and lxml, but I'm not sure if what I seek to do is possible via this route.
Any suggestions or experiences to share would be greatly appreciated. I feel I've read every article that is easily discoverable through a google search at least 5 times.
Let me know if anything needs clarification.
Some things to note:
I started getting into coding about 2 months ago. I’ve been doing it intensively for that time and I feel I’m picking up the essential concepts, but there are severe gaps in my knowledge.
Here are my tools:
Yosemite 10.10,
Microsoft Office 2011 for Mac
You probably need to be more specific, but the short answer is, in principle, yes.
At a certain level, all python-docx does is modify strings in the XML. A couple things though:
The XML you create needs to remain well-formed and valid according to the schema. So if you change the text enclosed in a <w:t> element, for example, that works fine. Conversely, if you inject a bunch of random XML at an arbitrary point in one of the .xml parts, that will corrupt the file.
The XML "files", known as parts that make up a .docx file are contained in a Zip archive known as a package. You must unpackage and repackage that set of parts properly in order to have a valid .docx file afterward. python-docx takes care of all those details for you, but if you're going directly at the .docx file you'll need to take care of that yourself.
I am trying to map one xml file to another, based on a configuration file (that too can be an xml file).
Input
<ia>
<ib>...</ib>
<ic>...</ic>
</ia>
Output
<oa>
<ob>...</ob>
<oc>...</oc>
</oa>
Config
<config>
<conf>
<input>ia</input>
<output>oa</output>
</conf>
<conf>
<input>ib</input>
<output>ob</output>
</conf>
.....
</config>
So, the intention is to parse an xml file and retrieve the information interesting to me, and write into another xml file where the mapping information is specified in the config file.
Due to the scripting nature (and extending with plugins lateron), and the support for xml processing I was considering python. I just learned the syntax and basics of language, and came to know about lxml
One way of doing this
parse the config file (where , tag can have xpath to the node I am interested in)
read the input file
write into output, using etbuilder based on the config file
Being new to python, and not seeing xpath support for etbuilder I wonder is this the best approach. Also not sure all the exceptional cases. Is there an easier way, or native support in any other libraries. If possible, I do not want to spend too much time on this task as I could focus on the core task.
thanks ins advance.
If you wish to transform an XML file into another XML file then XSLT was made for this purpose. You have to define a .xslt file that describes the transformation of XML content and what the eventual output should look like. That's one way to do it.
You can also read the XML file using lxml and generate the output XML with lxml.etree.ElementTree. I'm not familiar with etbuilder but I don't think generating the desired output is that difficult. Once you have parsed the input files, you can build the config XML and write it to a file.
XPath is primarily for reading XML content, you don't need it for constructing XML files. In fact, if you use a proper XML parser then you don't need XPath either to read the file contents, although XPath could make life a bit easier.