Convert HTM Tables to CSV

What changes when you turn an HTM or HTML table into CSV, what is lost, how to keep data intact, and when CSV is the wrong format.

An .htm file is an HTML document; the shorter extension is a holdover from older systems and does not change how the file works. Converting its tables to CSV means pulling the cell contents out of the page markup and writing them as plain rows of comma-separated values. This review covers what that conversion keeps, what it discards, and how to tell whether the result will hold up for your purpose.

Evidence note: no specific converter, test results or measurements were supplied for this article. The guidance below rests on how HTML tables and the CSV format are defined and commonly handled. It does not rank or vouch for particular tools.

Is HTM-to-CSV conversion right for your use?

The main benefit is portability. CSV opens in spreadsheets, databases, scripting languages and analysis tools without a browser or parser for page markup. If you need to sort, filter, import or compute on tabular data that currently lives in a web page, CSV is usually the shortest path.

The decisive tradeoff is that CSV holds only a flat grid of text. Anything that depends on the page's structure or presentation is dropped or flattened. This includes merged cells, captions, header relationships, links, emphasis and nested tables. Whether that matters depends on whether the meaning of the table lives in the values alone or also in its layout.

  • CSV is a good fit when:
  • Each row is one record and each column is one field.
  • The table has a single header row and no merged cells.
  • You plan to import the data into a spreadsheet, database or script.
  • Keep the HTML, or export to a richer format, when:
  • The table uses rowspan or colspan to group values, and that grouping carries meaning.
  • Links, footnote markers or inline formatting are part of the content.
  • Readers need the caption, summary or multi-level headers to interpret the numbers.
  • The output is meant to be read by people rather than processed by software.

Plain text or the original page is preferable when the goal is reading, not processing. A CSV file viewed raw is harder to scan than a rendered table. It also gives screen reader users less context than a properly marked-up HTML table.

What the conversion actually changes

This is a structural extraction, not a character-level transformation. It does not reverse, mirror or restyle text, and it does not substitute decorative Unicode characters. The characters inside each cell should come through unchanged. What changes is the container around them.

A converter reads each table row element as a row and each header or data cell as a field. It then writes the fields separated by commas, with rows separated by line breaks. The widely referenced CSV specification, RFC 4180, sets the quoting rules. A field containing a comma, a double quote or a line break must be wrapped in double quotes. Any double quote inside the field is written twice.

Illustrative input, written for this article rather than taken from a tool:

<table>
  <caption>Q3 orders</caption>
  <thead>
    <tr><th>Order ID</th><th>Customer</th><th>Note</th></tr>
  </thead>
  <tbody>
    <tr><td>00417</td><td>Lee, Dana</td><td>Ships "as is"</td></tr>
    <tr><td>00418</td><td>Ortiz</td><td><a href="/r/418">Returned</a></td></tr>
  </tbody>
</table>

A correct CSV rendering of that table, following the quoting rules above:

Order ID,Customer,Note
00417,"Lee, Dana","Ships ""as is"""
00418,Ortiz,Returned
  • What happened in that example:
  • The caption "Q3 orders" disappeared, because CSV has no place for a title.
  • The header cells became an ordinary first row. CSV does not mark which row is the header.
  • "Lee, Dana" was quoted because it contains a comma.
  • The quotes around "as is" were doubled, and the whole field was quoted.
  • The link text "Returned" survived, but the link target was discarded.
  • "00417" is stored as text with its leading zeros. Whether it stays that way depends on the program that opens the file.

Merged cells need particular attention. A cell spanning two rows or columns exists once in the HTML but must fill two positions in a grid. Converters handle this differently. Some repeat the value in every covered position, some leave the extra positions empty, and some misalign the remaining columns. Check any table that uses spans before relying on the output.

Pages with several tables, including tables used purely for layout, may produce multiple outputs or pick up content you did not intend. Nested tables are usually flattened into the parent cell's text, or skipped.

If you work with scripts, the pandas library's read_html function parses every table in an HTML file into a separate data frame. It depends on an HTML parser such as lxml or html5lib being installed. Each frame can then be written out with to_csv. A minimal sketch:

import pandas as pd

tables = pd.read_html("page.htm")
tables[0].to_csv("orders.csv", index=False)

Be aware that read_html infers column types. A column like Order ID may be read as integers, which drops the leading zeros before the CSV is ever written. You can prevent this by passing a converters argument that keeps that column as strings.

Will the CSV stay readable and usable after sharing?

The CSV file itself is plain text and copies cleanly. Most problems appear when another program interprets it.

  • Common points of failure:
  • Type guessing. Spreadsheet programs often reinterpret values on open. They can strip leading zeros from IDs and ZIP codes, turn part numbers into dates, or round long numbers. Importing through the spreadsheet's import dialog, with columns set to text, avoids this. Opening the file by double-clicking usually does not.
  • Character encoding. Save as UTF-8. Some spreadsheet versions misread UTF-8 without a byte-order mark, so accented letters and non-Latin scripts turn into garbled sequences. If the file looks wrong in one program but right in a text editor, the problem is encoding, not the data.
  • Delimiter and locale. In regions where the comma is the decimal separator, some software expects semicolons between fields. A comma-delimited file may then load as a single column. Tab-separated values (TSV) avoid the clash if the destination accepts them.
  • Line breaks inside cells. A cell containing a line break is valid CSV when quoted. Simple line-by-line tools and some importers still split it into two rows.

Formula injection is a safety concern when the source page is not under your control. If a cell begins with =, +, - or @, a spreadsheet may treat it as a formula when the CSV is opened. Security guidance commonly recommends neutralizing such values before sharing extracted data. One approach is prefixing them with a single quote.

Right-to-left text, such as Arabic or Hebrew, is stored in CSV in logical reading order, just as in the HTML. How it displays depends on the viewer's bidirectional text support, not on the conversion. A raw text editor may show mixed-direction cells in an order that looks scrambled even though the stored data is correct.

For accessibility, the conversion is a step down from well-built HTML. A marked-up table can tell assistive technology which header applies to each cell and announce the caption. A CSV file carries none of that. Spreadsheet programs may restore some navigation once the file is opened, but how well that works with a given screen reader was not tested for this article. If people will read the data, share the original table or an accessible spreadsheet alongside the CSV.

Searching and editing generally improve after conversion. The values become plain text that any editor, spreadsheet or command-line tool can search, sort and change. You no longer have to work around tags.

Choosing and checking a converter

Online converters, browser extensions, spreadsheet import features and scripts can all do this job. None were evaluated for this article. If the table contains personal, financial or confidential data, do not assume an online tool deletes uploads or keeps them private because of how it looks or what it advertises. Read its stated policy, or convert locally with a spreadsheet or script.

  • A quick verification routine for any output:
  • Compare row and column counts against the original table.
  • Check rows next to merged cells for shifted columns.
  • Open the CSV in a plain text editor to confirm quoting and encoding before opening it in a spreadsheet.
  • Spot-check values with leading zeros, long numbers, dates and non-English characters.
  • Confirm that captions, links or footnotes you need have been kept somewhere, or recorded separately.

Bottom line

Converting HTM tables to CSV preserves cell text reliably and makes the data far easier to process. It discards structure and presentation. For simple record-style tables headed into a spreadsheet, database or script, it is the right move, provided you control encoding and type guessing on import. For tables with merged cells, meaningful links or layered headers, or for output that people will read, keep the HTML or export to a richer format instead.