Edit HTML, inserting CSS reference
<p>It's possible to create HTML page from a CSV file, with the following:</p>

<pre><code>import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

df = pd.read_csv('../data.csv',delimiter=';', engine='python')
df.to_html('csv.html')
</code></pre>

<p>I would like to make that HTML to respect some CSS present in <code>csv.css</code>. One way to get this is to manually edit <code>csv.html</code> <code>head</code>, inserting:</p>

<pre><code>&lt;head&gt;&lt;link rel="stylesheet" type="text/css" href="csv.css"&gt;&lt;/head&gt; 
</code></pre>

<p>Instead of doing it manually, how one can get there programmatically (using Python)?</p>

<p>The <code>to_html</code> method does not output an entire HTML document. Instead, it just creates a single <code>table</code> element.</p>

<p>If you want to include CSS, you have to create additional HTML elements, and insert them yourself before writing the data. One of the simplest ways is as follows:</p>

<pre><code>with open('test.html', 'w') as fobj:
    fobj.write('&lt;html&gt;&lt;head&gt;&lt;link rel="stylesheet" href="test.css"&gt;&lt;/head&gt;&lt;body&gt;')
    df.to_html(fobj)
    fobj.write('&lt;/body&gt;&lt;/html&gt;')
</code></pre>

<p>The first argument of <code>to_html</code> has to be a file-like object: so it can be either a file object as in the above example or a <a href="https://docs.python.org/3/library/io.html#io.StringIO" rel="nofollow"><code>StringIO</code></a>.</p>

python html python-2.7 pandas