I have Excel files that I'm converting into HTML tables. As part of this conversion, there are columns that are grouped together like item C in the table below.
+---+---+
| A | B |
+---+---+
| | D |
+ C +---+
| | E |
+---+---+
| G | H |
+---+---+
In my final HTML, I'd like to apply a rowspan to those columns so that the table displays the same as the Excel file. As far as I can gather, there isn't a way to do this with Python. There is a way to do it with JavaScript and I've stumbled upon code that works perfectly as confirmed by JSFiddle.
function tableCheck() {
const table = document.querySelector("table");
let headerCell = null;
for (let row of table.rows) {
const firstCell = row.cells[0];
if (headerCell === null || firstCell.innerText !== headerCell.innerText) {
headerCell = firstCell;
} else {
headerCell.rowSpan++;
firstCell.remove();
}
}
}
tableCheck()
The problem is that I cannot figure out how to apply this using js2py. I imagine I'm not passing the HTML file contents correctly into the JS function, but so far my guesses and searches have been unsuccessful.
My latest attempt is below. Any tips on how to get the JavaScript to work? Is there a way to do this with Python that I missed? Anything else I can provide to help describe expected output? Thank you!
import pandas as pd
import js2py
from js2py import eval_js
xlsx = pd.ExcelFile(filename)
sheetNumbers = len(xlsx.sheet_names)
for i in range (sheetNumbers):
first_info = "blah"
last_info = "blahblah"
df = pd.read_excel(filename, sheet_name=i)
df = df.fillna(method = 'ffill') # fills merged cells (single column) with merged value
df.to_html("excelAsHTML.html", index=False)
with open("excelAsHTML.html") as file:
file = file.read()
js = """
function tableCheck() {
const table = document.querySelector("table");
let headerCell = null;
for (let row of table.rows) {
const firstCell = row.cells[0];
if (headerCell === null || firstCell.innerText !== headerCell.innerText) {
headerCell = firstCell;
} else {
headerCell.rowSpan++;
firstCell.remove();
}
}
}
tableCheck(file)
"""
file = js2py.eval_js(js)
with open("excelAsHTML.txt", "a") as file_to_write:
file_to_write.write(first_info + file + last_info)