Does the E-factory of lxml support dynamically generated data?

2024/9/8 10:10:15

Is there a way of creating the tags dynamically with the E-factory of lxml? For instance I get a syntax error for the following code:

E.BODY(E.TABLE(for row_num in range(len(ws.rows)):row = ws.rows[row_num]# create a tr tagE.TR(for cell_num in range(len(row)):cell = row[cell_num]

I get the following error:

   for row_num in range(len(ws.rows)):^SyntaxError: invalid syntax
Answer

In order to create multiple child nodes, pass multiple positional or keyword arguments.

Working example:

from lxml.builder import ElementMaker
from lxml.html import tostringE = ElementMaker()body = E.BODY(E.TABLE(*[E.TR(*[E.TD("%s %s" % (row_num, col_num)) for col_num in range(3)]) for row_num in range(2)])
)print tostring(body, pretty_print=True)

Prints:

<BODY><TABLE>
<TR>
<TD>0 0</TD>
<TD>0 1</TD>
<TD>0 2</TD>
</TR>
<TR>
<TD>1 0</TD>
<TD>1 1</TD>
<TD>1 2</TD>
</TR>
</TABLE></BODY>

As a side note, from what I understand you want to create an HTML file filled with data coming from a parsed excel file. Instead of making elements with lxml, you might better and easier solve it with a template engine like jinja2 or mako.

https://en.xdnf.cn/q/72819.html

Related Q&A

Check if datetime object in pandas has a timezone?

Im importing data into pandas and want to remove any timezones – if theyre present in the data. If the data has a time zone, the following code works successfully: col = "my_date_column" df[…

Extract translator comments with xgettext from JavaScript (in Python mode)

I have a pretty well-working command that extracts strings from all my .js and .html files (which are just Underscore templates). However, it doesnt seem to work for Translator comments.For example, I …

Embedding python + numpy code into C++ dll callback

I am new of python embedding. I am trying to embed python + numpy code inside a C++ callback function (inside a dll)the problem i am facing is the following. if i have:Py_Initialize(); // some python g…

How to parse single file using Python bindings to Clang?

I am writing a simple tool to help with refactoring the source code of our application. I would like to parse C++ code based on wxWidgets library, which defines GUI and produce XML .ui file to use with…

How can I profile a Kivy application?

Im building a game using Kivy. Im encountering performance issues so I decided to profile the program.I tried to run it by:python -m cProfile main.pyThe application screen stays black. After several se…

Set up multiple python installations on windows with tox

I am trying to set up tox on windows to run tests against multiple python installations. I have installed each python in folders named, C:\Python\PythonXX_YY, XX is the python version (e.g. 27) and YY…

How can I change the alpha value dynamically in matplotlib python

Im seeking how to change an alpha value dynamically which are already plotted.This is a kind of sample code I want to implement, but I know it is a wrong writing.import matplotlib.pyplot as pltfig = pl…

How to detect write failure in asyncio?

As a simple example, consider the network equivalent of /dev/zero, below. (Or more realistically, just a web server sending a large file.)If a client disconnects early, you get a barrage of log message…

how to get place details from place id in google places api for python

I am using the Google Places API with Python to build a collective intelligence app for food. e.g. what restaurants are around, what ratings they have, what are their timings, etc.I am doing the follow…

pandas.algos._return_false causes PicklingError with dill.dump_session on CentOS

I have a code framework which involves dumping sessions with dill. This used to work just fine, until I started to use pandas. The following code raises a PicklingError on CentOS release 6.5:import pan…