Create csvconvertor.py
This commit is contained in:
parent
041be54471
commit
9f9fb6bf63
1 changed files with 32 additions and 0 deletions
32
packages/markitdown/src/markitdown/csvconvertor.py
Normal file
32
packages/markitdown/src/markitdown/csvconvertor.py
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
# File: packages/markitdown/csv_converter.py
|
||||
|
||||
import csv
|
||||
import io
|
||||
from .document_converter import DocumentConverter, DocumentConverterResult
|
||||
|
||||
class CsvConverter(DocumentConverter):
|
||||
def accepts(self, stream_info):
|
||||
return (
|
||||
stream_info.extension == ".csv"
|
||||
or stream_info.mime_type in ["text/csv", "application/csv"]
|
||||
)
|
||||
|
||||
def convert(self, stream_info):
|
||||
content = stream_info.read_text()
|
||||
reader = csv.reader(io.StringIO(content))
|
||||
rows = list(reader)
|
||||
|
||||
if not rows:
|
||||
return DocumentConverterResult("")
|
||||
|
||||
header = rows[0]
|
||||
table = []
|
||||
table.append("| " + " | ".join(header) + " |")
|
||||
table.append("| " + " | ".join(["---"] * len(header)) + " |")
|
||||
|
||||
for row in rows[1:]:
|
||||
row += [""] * (len(header) - len(row)) # Pad missing cells
|
||||
table.append("| " + " | ".join(row[:len(header)]) + " |")
|
||||
|
||||
markdown_output = "\n".join(table)
|
||||
return DocumentConverterResult(markdown_output)
|
||||
Loading…
Reference in a new issue