Home > Mobile >  Pandas save to excel with title
Pandas save to excel with title

Time:03-29

HTML file content: here

I'm reading HTML page from a file using pandas successfully but am looking to assign the title which i extracted above the columns within center before saving the output to excel file.

Below is my code:

import pandas as pd
from bs4 import BeautifulSoup

with open('r.html') as f:
    soup = BeautifulSoup(f.read(), 'lxml')
    title = '\n'.join([x.text for x in soup.select('span.valor', limit=2)])

df = pd.read_html('r.html')[0]
df.style.set_caption(title)
print(df)
df.to_excel('test.xlsx', index=False)

CodePudding user response:

Is this what you mean?

import pandas as pd
from bs4 import BeautifulSoup
with open('r.html') as f:
    soup = BeautifulSoup(f.read(), 'lxml')
    title = '\n'.join([x.text for x in soup.select('span.valor', limit=2)])

df = pd.read_html('r.html')[0]

writer = pd.ExcelWriter('r.xlsx')
df.to_excel(writer, startcol = 0, startrow = 1)
worksheet = writer.sheets['Sheet1']
worksheet.write_string(0, 0, title)
writer.save()
  • Related