简体   繁体   中英

How can I replace HTML code inside an excel sheet using Python and openpyxl?

I want to replace HTML code in an excel sheet with openpyxl. For example I want to replace &#43; with + or &lt; with < or &gt; with > . How can I replace all occurences in my sheet at once? Is there any library available?

Thanks very much for your help.

I just found contributions about replacing HTML tags with RegEx, not HTML code itself.

Openpyxl works on cells not the whole sheet.
You can use the html module to modify html code within a cell. For each cell you want to change, update the value using html.unescape ;
Example

...
import html

value_in_cell = cell.value
### For example if the returned value_in_cell is;   
value_in_cell = '&#43;&lt;html string&gt'
es_string = html.unescape(value_in_cell)

'es_string' in this example code will be converted to

+<html string>

which can be updated in the cell.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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