CSV is simple, portable, and everywhere—but raw grids rarely tell the story on their own. Converting CSV into richer formats helps you present, publish, integrate, or load data into apps and databases. Below is a practical, example‑driven guide you can use right away.
Starter dataset used in examples
Here’s a tiny CSV we would convert in multiple ways:
order_id,customer,amount,date,paid
1001,Asha,49.99,2025-07-31,true
1002,Omar,125.00,2025-08-01,false
1003,Meera,75.50,2025-08-01,true
CSV to PDF
Use this when you need a polished, shareable report: invoices, statements, dashboards.
Approaches
Direct table rendering: Quick export of a table with minimal styling.
HTML-to-PDF pipeline: Generate an HTML report (with CSS) then print to PDF for pixel‑perfect control.
Programmatic report layouts: Build page headers/footers, pagination, totals, and charts in code.
`import pandas as pd
from weasyprint import HTML, CSS
df = pd.read_csv("orders.csv")
html = f"""
<!doctype html>
<br> body {{ font-family: Arial, sans-serif; margin: 24px; }}<br> h1 {{ margin-bottom: 8px; }}<br> table {{ border-collapse: collapse; width: 100%; }}<br> th, td {{ border: 1px solid #ddd; padding: 8px; font-size: 12px; }}<br> th {{ background: #f5f5f5; text-align: left; }}<br> tfoot td {{ font-weight: bold; }}<br>
Orders Report
{df.to_html(index=False)}
"""
HTML(string=html).write_pdf("orders.pdf")
`
What it does: Reads CSV, renders a styled HTML table, prints it to PDF.
Why it’s nice: Full CSS support; easy to brand.
Node.js (Puppeteer: HTML template → PDF)
`const fs = require('fs');
const Papa = require('papaparse');
const puppeteer = require('puppeteer');
(async () => {
const csv = fs.readFileSync('orders.csv', 'utf8');
const { data } = Papa.parse(csv, { header: true, dynamicTyping: true });
const rows = data.map(r =>
).join('');
<tr>
<td>${r.order_id}</td><td>${r.customer}</td>
<td>${r.amount}</td><td>${r.date}</td><td>${r.paid}</td>
</tr>
const html =
;
<html><head><style>
body { font-family: Arial, sans-serif; margin: 24px; }
table { border-collapse: collapse; width: 100%; }
th, td { border: 1px solid #ddd; padding: 8px; font-size: 12px; }
th { background: #f5f5f5; text-align: left; }
</style></head><body>
<h1>Orders Report</h1>
<table>
<thead><tr><th>order_id</th><th>customer</th><th>amount</th><th>date</th><th>paid</th></tr></thead>
<tbody>${rows}</tbody>
</table>
</body></html>
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.setContent(html, { waitUntil: 'networkidle0' });
await page.pdf({ path: 'orders.pdf', format: 'A4', printBackground: true });
await browser.close();
})();
`
CLI (CSV → HTML → PDF)
CSV to HTML (csvkit): csvlook -H orders.csv | sed 's/|/
Pragmatic path:
Use a simple template that embeds a generated HTML table (from a script or pandas), then
Convert with wkhtmltopdf: wkhtmltopdf report.html orders.pdf
For complex PDFs (headers, footers, page numbers), prefer HTML+CSS → PDF engines over pure PDF libraries.
CSV to HTML
Perfect for dashboards, documentation, and web embeds.
Approaches
Pandas one-liner: Quick table for internal pages.
Static site build: Use a template and inject table rows.
Interactive table: Add sorting, filtering, pagination with a JS library.
Examples
Python (pandas → HTML table)
import pandas as pd
df = pd.read_csv("orders.csv")
html = df.to_html(index=False, classes="table table-sm", border=0)
open("orders.html", "w").write(f"""
<!doctype html><html><head>
<meta charset="utf-8">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/water.css@2/out/water.css">
</head><body>
<h1>Orders</h1>
{html}
</body></html>
""")
Vanilla HTML + Papa Parse (client-side)
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>Orders</title>
<script src="https://cdn.jsdelivr.net/npm/papaparse@5.4.1/papaparse.min.js"></script>
<style>
table { border-collapse: collapse; width: 100%; }
th, td { border: 1px solid #ddd; padding: 8px; font-size: 12px; }
th { background: #f5f5f5; text-align: left; }
</style>
</head>
<body>
<h1>Orders</h1>
<table id="tbl"><thead></thead><tbody></tbody></table>
<script>
Papa.parse('orders.csv', {
download: true, header: true, dynamicTyping: true,
complete: ({ data, meta }) => {
const thead = document.querySelector('#tbl thead');
const tbody = document.querySelector('#tbl tbody');
thead.innerHTML = '<tr>' + meta.fields.map(h =>
).join('') + '</tr>';
tbody.innerHTML = data.map(r =>
'<tr>' + meta.fields.map(h =>
${r[h] ?? ''}
).join('') + '</tr>'
).join('');
}
});
</script>
</body>
</html>
Tuesday
write a detailed post on CSV to PDF, HTML, JSON and SQL converters. Pleae quote some examples too.
Converting CSV to PDF, HTML, JSON, and SQL
CSV is simple, portable, and everywhere—but raw grids rarely tell the story on their own. Converting CSV into richer formats helps you present, publish, integrate, or load data into apps and databases. Below is a practical, example‑driven guide you can use right away.
Starter dataset used in examples
Here’s a tiny CSV we’ll convert in multiple ways:
order_id,customer,amount,date,paid
1001,Asha,49.99,2025-07-31,true
1002,Omar,125.00,2025-08-01,false
1003,Meera,75.50,2025-08-01,true
CSV to PDF
Use this when you need a polished, shareable report: invoices, statements, dashboards.
Approaches
Direct table rendering: Quick export of a table with minimal styling.
HTML-to-PDF pipeline: Generate an HTML report (with CSS) then print to PDF for pixel‑perfect control.
Programmatic report layouts: Build page headers/footers, pagination, totals, and charts in code.
Examples
Python (pandas + WeasyPrint)
python
import pandas as pd
from weasyprint import HTML, CSS
df = pd.read_csv("orders.csv")
html = f"""
<!doctype html>
<br> body {{ font-family: Arial, sans-serif; margin: 24px; }}<br> h1 {{ margin-bottom: 8px; }}<br> table {{ border-collapse: collapse; width: 100%; }}<br> th, td {{ border: 1px solid #ddd; padding: 8px; font-size: 12px; }}<br> th {{ background: #f5f5f5; text-align: left; }}<br> tfoot td {{ font-weight: bold; }}<br>
Orders Report
{df.to_html(index=False)}
"""
HTML(string=html).write_pdf("orders.pdf")
What it does: Reads CSV, renders a styled HTML table, prints it to PDF.
Why it’s nice: Full CSS support; easy to brand.
Node.js (Puppeteer: HTML template → PDF)
js
const fs = require('fs');
const Papa = require('papaparse');
const puppeteer = require('puppeteer');
(async () => {
const csv = fs.readFileSync('orders.csv', 'utf8');
const { data } = Papa.parse(csv, { header: true, dynamicTyping: true });
const rows = data.map(r =>
).join('');
<tr>
<td>${r.order_id}</td><td>${r.customer}</td>
<td>${r.amount}</td><td>${r.date}</td><td>${r.paid}</td>
</tr>
const html =
;
<html><head><style>
body { font-family: Arial, sans-serif; margin: 24px; }
table { border-collapse: collapse; width: 100%; }
th, td { border: 1px solid #ddd; padding: 8px; font-size: 12px; }
th { background: #f5f5f5; text-align: left; }
</style></head><body>
<h1>Orders Report</h1>
<table>
<thead><tr><th>order_id</th><th>customer</th><th>amount</th><th>date</th><th>paid</th></tr></thead>
<tbody>${rows}</tbody>
</table>
</body></html>
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.setContent(html, { waitUntil: 'networkidle0' });
await page.pdf({ path: 'orders.pdf', format: 'A4', printBackground: true });
await browser.close();
})();
CLI (CSV → HTML → PDF)
CSV to HTML (csvkit): csvlook -H orders.csv | sed 's/|/
Pragmatic path:
Use a simple template that embeds a generated HTML table (from a script or pandas), then
Convert with wkhtmltopdf: wkhtmltopdf report.html orders.pdf
Tip: For complex PDFs (headers, footers, page numbers), prefer HTML+CSS → PDF engines over pure PDF libraries.
CSV to HTML
Perfect for dashboards, documentation, and web embeds.
Approaches
Pandas one-liner: Quick table for internal pages.
Static site build: Use a template and inject table rows.
Interactive table: Add sorting, filtering, pagination with a JS library.
Examples
Python (pandas → HTML table)
python
import pandas as pd
df = pd.read_csv("orders.csv")
html = df.to_html(index=False, classes="table table-sm", border=0)
open("orders.html", "w").write(f"""
<!doctype html>
Orders
{html}
""")
Vanilla HTML + Papa Parse (client-side)
html
<!doctype html>
Orders
<br>
table { border-collapse: collapse; width: 100%; }<br>
th, td { border: 1px solid #ddd; padding: 8px; font-size: 12px; }<br>
th { background: #f5f5f5; text-align: left; }<br>
Orders
<br>
Papa.parse('orders.csv', {<br>
download: true, header: true, dynamicTyping: true,<br>
complete: ({ data, meta }) => {<br>
const thead = document.querySelector('#tbl thead');<br>
const tbody = document.querySelector('#tbl tbody');<br>
thead.innerHTML = '<tr>' + meta.fields.map(h => <code><th>${h}</th></code>).join('') + '</tr>';<br>
tbody.innerHTML = data.map(r =><br>
'<tr>' + meta.fields.map(h => <code><td>${r[h] ?? ''}</td></code>).join('') + '</tr>'<br>
).join('');<br>
}<br>
});<br>
CSV to JSON
Use this to feed APIs, store config/state, or work with JavaScript apps.
Mapping choices
Flat array of objects: Each CSV row becomes one JSON object.
Type inference: Convert numbers, booleans, and nulls—don’t keep everything as strings.
Grouping/nesting: Build nested structures by grouping columns (e.g., address_* → address object).
Examples
Output (flat array)
[
{"order_id":1001,"customer":"Asha","amount":49.99,"date":"2025-07-31","paid":true},
{"order_id":1002,"customer":"Omar","amount":125.0,"date":"2025-08-01","paid":false},
{"order_id":1003,"customer":"Meera","amount":75.5,"date":"2025-08-01","paid":true}
]
Python (pandas)
import pandas as pd
df = pd.read_csv("orders.csv", true_values=["true"], false_values=["false"])
df.to_json("orders.json", orient="records")
Node (Papa Parse)
const fs = require('fs');
const Papa = require('papaparse');
const csv = fs.readFileSync('orders.csv', 'utf8');
const { data } = Papa.parse(csv, { header: true, dynamicTyping: true });
fs.writeFileSync('orders.json', JSON.stringify(data, null, 2));
If you need nested JSON, split column names on a delimiter (e.g., address.city) and build objects recursively.
CSV to SQL
Load into databases for analysis, joins, dashboards, or transactional apps.
Mapping choices
Schema inference: Guess column types (INTEGER, DECIMAL, DATE, BOOLEAN, TEXT).
Identifiers: Sanitize column names to valid SQL identifiers.
Loading path: INSERT statements vs. bulk COPY/LOAD for speed.
Examples
Inferred schema and inserts (SQLite syntax
`CREATE TABLE orders (
order_id INTEGER,
customer TEXT,
amount REAL,
date TEXT,
paid BOOLEAN
);
INSERT INTO orders (order_id, customer, amount, date, paid) VALUES
(1001, 'Asha', 49.99, '2025-07-31', 1),
(1002, 'Omar', 125.00, '2025-08-01', 0),
(1003, 'Meera', 75.50, '2025-08-01', 1);
# Preview inferred schema as a CREATE TABLE statement
csvkit (CLI) — infer CREATE TABLE and SQL
csvsql --dialect sqlite --tables orders --no-create --insert orders.csv > inserts.sql
Or generate a CREATE TABLE statement
csvsql --dialect sqlite --tables orders orders.csv > create_table.sql
`
PostgreSQL bulk load
-- In psql shell:
\copy orders(order_id, customer, amount, date, paid) FROM 'orders.csv' CSV HEADER;
Python (pandas → SQLAlchemy)
`import pandas as pd
from sqlalchemy import create_engine
df = pd.read_csv("orders.csv", true_values=["true"], false_values=["false"])
engine = create_engine("sqlite:///example.db")
df.to_sql("orders", engine, if_exists="replace", index=False)
`
For very large CSVs, prefer bulk loaders (PostgreSQL COPY, MySQL LOAD DATA, SQL Server BULK INSERT) over INSERT loops