Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion docreader/parser/csv_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,14 @@ def convert(self, file_stream, stream_info, **kwargs):
if stream_info.charset
else str(from_bytes(data).best())
)
rows = list(csv.reader(io.StringIO(content)))
# A BOM survives the decode and would end up inside the first header
# cell; strip it before the table is built.
rows = list(csv.reader(io.StringIO(content.lstrip("\ufeff"))))
# Exported sheets often start with a blank line. Taking that line as the
# header leaves a width of 0, which turns every cell of the table into an
# empty string and drops the whole document.
while rows and not any(cell.strip() for cell in rows[0]):
rows.pop(0)
if not rows:
return DocumentConverterResult(markdown="")
width = len(rows[0])
Expand Down
18 changes: 18 additions & 0 deletions docreader/tests/test_csv_cell_boundaries.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,21 @@ def test_encoding_and_plain_inputs_keep_their_values(self):
Parser().parse_file("plain.csv", "csv", b"A,B\nx,2\n").content,
"| A | B |\n| --- | --- |\n| x | 2 |",
)

def test_leading_blank_line_does_not_eat_the_table(self):
data = b"\r\nname,age\r\nAlice,30\r\n"
for engine in ["", "builtin", "markitdown"]:
with self.subTest(engine=engine):
result = Parser().parse_file("blank.csv", "csv", data, engine)
self.assertEqual(
result.content, "| name | age |\n| --- | --- |\n| Alice | 30 |"
)

def test_bom_is_not_part_of_the_first_header_cell(self):
data = "name,age\r\nAlice,30\r\n".encode("utf-8-sig")
for engine in ["", "builtin", "markitdown"]:
with self.subTest(engine=engine):
result = Parser().parse_file("bom.csv", "csv", data, engine)
self.assertEqual(
result.content, "| name | age |\n| --- | --- |\n| Alice | 30 |"
)
Loading