chore: finer flags, forward na_rep
This commit is contained in:
parent
c2aae4ddda
commit
5c60d8ca12
2 changed files with 22 additions and 22 deletions
|
|
@ -523,19 +523,18 @@ class XlsxConverter(HtmlConverter):
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def _clean_colname(self, colname: Any) -> Any:
|
def _clean_colname(self, colname: Any) -> Any:
|
||||||
|
# Remove Pandas header placeholders
|
||||||
if isinstance(colname, str) and colname.startswith("Unnamed:"):
|
if isinstance(colname, str) and colname.startswith("Unnamed:"):
|
||||||
return ""
|
return ""
|
||||||
return colname
|
return colname
|
||||||
|
|
||||||
def _clean_dataframe(self, df: pd.DataFrame) -> pd.DataFrame:
|
|
||||||
return (
|
|
||||||
df.rename(columns=lambda col: self._clean_colname(col))
|
|
||||||
.dropna(how="all", axis=1)
|
|
||||||
.dropna(how="all", axis=0)
|
|
||||||
)
|
|
||||||
|
|
||||||
def convert(
|
def convert(
|
||||||
self, local_path, beautify: bool = True, **kwargs
|
self,
|
||||||
|
local_path,
|
||||||
|
na_rep: Any = "",
|
||||||
|
drop_empty_cols: bool = False,
|
||||||
|
drop_empty_rows: bool = False,
|
||||||
|
**kwargs,
|
||||||
) -> Union[None, DocumentConverterResult]:
|
) -> Union[None, DocumentConverterResult]:
|
||||||
# Bail if not a XLSX
|
# Bail if not a XLSX
|
||||||
extension = kwargs.get("file_extension", "")
|
extension = kwargs.get("file_extension", "")
|
||||||
|
|
@ -546,12 +545,15 @@ class XlsxConverter(HtmlConverter):
|
||||||
md_content = ""
|
md_content = ""
|
||||||
for name, sheet in sheets.items():
|
for name, sheet in sheets.items():
|
||||||
md_content += f"## {name}\n"
|
md_content += f"## {name}\n"
|
||||||
df = self._clean_dataframe(sheet) if beautify else sheet
|
sheet = sheet.rename(columns=lambda col: self._clean_colname(col))
|
||||||
html_content = (
|
|
||||||
df.to_html(index=False, na_rep="")
|
if drop_empty_cols:
|
||||||
if beautify
|
sheet = sheet.dropna(axis=1, how="all")
|
||||||
else df.to_html(index=False)
|
|
||||||
)
|
if drop_empty_rows:
|
||||||
|
sheet = sheet.dropna(axis=0, how="all")
|
||||||
|
|
||||||
|
html_content = sheet.to_html(index=False, na_rep=na_rep)
|
||||||
md_content += self._convert(html_content).text_content.strip() + "\n\n"
|
md_content += self._convert(html_content).text_content.strip() + "\n\n"
|
||||||
|
|
||||||
return DocumentConverterResult(
|
return DocumentConverterResult(
|
||||||
|
|
|
||||||
|
|
@ -140,18 +140,16 @@ def test_markitdown_local() -> None:
|
||||||
markitdown = MarkItDown()
|
markitdown = MarkItDown()
|
||||||
|
|
||||||
# Test XLSX processing
|
# Test XLSX processing
|
||||||
# XlsxConverter has an additional kwarg `beautify`, which defaults to True
|
result = markitdown.convert(os.path.join(TEST_FILES_DIR, "test.xlsx"))
|
||||||
result = markitdown.convert(
|
text_content = result.text_content.replace("\\", "")
|
||||||
os.path.join(TEST_FILES_DIR, "test.xlsx"), beautify=False
|
|
||||||
)
|
|
||||||
result_cleaned = markitdown.convert(os.path.join(TEST_FILES_DIR, "test.xlsx"))
|
|
||||||
# Check assertions
|
# Check assertions
|
||||||
for test_string in XLSX_TEST_STRINGS:
|
for test_string in XLSX_TEST_STRINGS:
|
||||||
text_content = result.text_content.replace("\\", "")
|
|
||||||
assert test_string in text_content
|
assert test_string in text_content
|
||||||
|
|
||||||
# Check negations
|
# Check negations
|
||||||
assert "Unnamed:" not in result_cleaned.text_content
|
assert "Unnamed:" not in result.text_content
|
||||||
assert "NaN" not in result_cleaned.text_content
|
assert "NaN" not in result.text_content
|
||||||
|
|
||||||
# Test DOCX processing
|
# Test DOCX processing
|
||||||
result = markitdown.convert(os.path.join(TEST_FILES_DIR, "test.docx"))
|
result = markitdown.convert(os.path.join(TEST_FILES_DIR, "test.docx"))
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue