Skip to content

Commit 41addf9

Browse files
committed
style: apply Black formatting to fix pre-commit checks
- Format all Python files with Black (v23.7.0) - Fix line length and formatting issues in page extraction feature files - Ensure consistent code style across the codebase
1 parent fd6aa68 commit 41addf9

8 files changed

Lines changed: 118 additions & 123 deletions

File tree

packages/markitdown/src/markitdown/__main__.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,8 @@ def main():
217217
)
218218
else:
219219
result = markitdown.convert(
220-
args.filename,
221-
stream_info=stream_info,
220+
args.filename,
221+
stream_info=stream_info,
222222
keep_data_uris=args.keep_data_uris,
223223
extract_pages=args.extract_pages,
224224
)
@@ -229,20 +229,17 @@ def main():
229229
def _handle_output(args, result: DocumentConverterResult):
230230
"""Handle output to stdout or file"""
231231
output_content = ""
232-
232+
233233
if args.extract_pages and result.pages and args.pages_json:
234234
# Output as JSON with page information
235235
pages_data = [
236-
{
237-
"page_number": page.page_number,
238-
"content": page.content
239-
}
236+
{"page_number": page.page_number, "content": page.content}
240237
for page in result.pages
241238
]
242239
output_data = {
243240
"markdown": result.markdown,
244241
"title": result.title,
245-
"pages": pages_data
242+
"pages": pages_data,
246243
}
247244
output_content = json.dumps(output_data, ensure_ascii=False, indent=2)
248245
elif args.extract_pages and result.pages:
@@ -251,15 +248,15 @@ def _handle_output(args, result: DocumentConverterResult):
251248
output_content += "\n\n" + "=" * 50 + "\n"
252249
output_content += f"EXTRACTED {len(result.pages)} PAGES:\n"
253250
output_content += "=" * 50 + "\n\n"
254-
251+
255252
for page in result.pages:
256253
output_content += f"--- PAGE {page.page_number} ---\n"
257254
output_content += page.content
258255
output_content += "\n\n"
259256
else:
260257
# Standard output
261258
output_content = result.markdown
262-
259+
263260
if args.output:
264261
with open(args.output, "w", encoding="utf-8") as f:
265262
f.write(output_content)

packages/markitdown/src/markitdown/_base_converter.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44

55
class PageInfo:
66
"""Information about a specific page in a document."""
7-
7+
88
def __init__(self, page_number: int, content: str):
99
"""
1010
Initialize page information.
11-
11+
1212
Parameters:
1313
- page_number: The page number (1-indexed)
1414
- content: The markdown content of the page

packages/markitdown/src/markitdown/_stream_info.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ class StreamInfo:
1111
mimetype: Optional[str] = None
1212
extension: Optional[str] = None
1313
charset: Optional[str] = None
14-
filename: Optional[
15-
str
16-
] = None # From local path, url, or Content-Disposition header
14+
filename: Optional[str] = (
15+
None # From local path, url, or Content-Disposition header
16+
)
1717
local_path: Optional[str] = None # If read from disk
1818
url: Optional[str] = None # If read from url
1919

packages/markitdown/src/markitdown/converters/_docx_converter.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,20 +77,22 @@ def convert(
7777

7878
# Check if page extraction is requested
7979
extract_pages = kwargs.get("extract_pages", False)
80-
80+
8181
style_map = kwargs.get("style_map", None)
8282
pre_process_stream = pre_process_docx(file_stream)
83-
83+
8484
# Convert to HTML
8585
html_result = mammoth.convert_to_html(pre_process_stream, style_map=style_map)
86-
86+
8787
# Convert HTML to markdown
8888
result = self._html_converter.convert_string(html_result.value, **kwargs)
89-
89+
9090
# Note: DOCX files don't have fixed pages like PDFs.
9191
# Page breaks depend on rendering settings (margins, font size, etc.)
9292
# For now, we'll return None for pages even if extract_pages is True
9393
# This maintains API compatibility while acknowledging the limitation
9494
pages = None
95-
96-
return DocumentConverterResult(markdown=result.markdown, title=result.title, pages=pages)
95+
96+
return DocumentConverterResult(
97+
markdown=result.markdown, title=result.title, pages=pages
98+
)

packages/markitdown/src/markitdown/converters/_pptx_converter.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def convert(
8080

8181
# Check if page extraction is requested
8282
extract_pages = kwargs.get("extract_pages", False)
83-
83+
8484
# Perform the conversion
8585
presentation = pptx.Presentation(file_stream)
8686
md_content = ""
@@ -157,7 +157,9 @@ def get_shape_content(shape, **kwargs):
157157

158158
# Tables
159159
if self._is_table(shape):
160-
slide_content += self._convert_table_to_markdown(shape.table, **kwargs)
160+
slide_content += self._convert_table_to_markdown(
161+
shape.table, **kwargs
162+
)
161163

162164
# Charts
163165
if shape.has_chart:
@@ -200,13 +202,15 @@ def get_shape_content(shape, **kwargs):
200202
if notes_frame is not None:
201203
slide_content += notes_frame.text
202204
slide_content = slide_content.strip()
203-
205+
204206
# Add to overall content
205207
md_content += slide_content
206-
208+
207209
# If extracting pages, add to pages list
208210
if extract_pages:
209-
pages.append(PageInfo(page_number=slide_num, content=slide_content.strip()))
211+
pages.append(
212+
PageInfo(page_number=slide_num, content=slide_content.strip())
213+
)
210214

211215
return DocumentConverterResult(markdown=md_content.strip(), pages=pages)
212216

packages/markitdown/tests/test_docx_page_extraction.py

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,112 +13,110 @@
1313

1414
class TestDocxPageExtraction:
1515
"""Test cases for DOCX page extraction functionality."""
16-
16+
1717
@pytest.fixture(autouse=True)
1818
def setup(self):
1919
"""Set up test fixtures."""
2020
self.markitdown = MarkItDown()
2121
self.test_docx_path = os.path.join(
22-
os.path.dirname(__file__),
23-
'test_files',
24-
'test.docx'
22+
os.path.dirname(__file__), "test_files", "test.docx"
2523
)
26-
24+
2725
def test_traditional_docx_conversion(self):
2826
"""Test that traditional DOCX conversion works unchanged."""
2927
if not os.path.exists(self.test_docx_path):
3028
pytest.skip("Test DOCX file not found")
31-
29+
3230
result = self.markitdown.convert(self.test_docx_path)
33-
31+
3432
# Verify result structure
3533
assert isinstance(result, DocumentConverterResult)
3634
assert isinstance(result.markdown, str)
3735
assert len(result.markdown) > 0
3836
assert result.pages is None # Should be None by default
39-
37+
4038
# Verify backward compatibility
41-
assert hasattr(result, 'text_content')
39+
assert hasattr(result, "text_content")
4240
assert result.text_content == result.markdown
4341
assert str(result) == result.markdown
44-
42+
4543
def test_docx_page_extraction_enabled(self):
4644
"""Test DOCX conversion with page extraction enabled.
47-
48-
Note: DOCX files don't have fixed pages like PDFs.
45+
46+
Note: DOCX files don't have fixed pages like PDFs.
4947
Page breaks depend on rendering settings. Currently,
5048
this returns None for pages even with extract_pages=True.
5149
"""
5250
if not os.path.exists(self.test_docx_path):
5351
pytest.skip("Test DOCX file not found")
54-
52+
5553
result = self.markitdown.convert(self.test_docx_path, extract_pages=True)
56-
54+
5755
# Verify result structure
5856
assert isinstance(result, DocumentConverterResult)
5957
assert isinstance(result.markdown, str)
6058
assert len(result.markdown) > 0
61-
59+
6260
# Currently, DOCX doesn't support page extraction due to dynamic pagination
6361
assert result.pages is None
64-
62+
6563
def test_docx_page_extraction_disabled(self):
6664
"""Test DOCX conversion with page extraction explicitly disabled."""
6765
if not os.path.exists(self.test_docx_path):
6866
pytest.skip("Test DOCX file not found")
69-
67+
7068
result = self.markitdown.convert(self.test_docx_path, extract_pages=False)
71-
69+
7270
# Should behave the same as default
7371
assert isinstance(result, DocumentConverterResult)
7472
assert isinstance(result.markdown, str)
7573
assert len(result.markdown) > 0
7674
assert result.pages is None
77-
75+
7876
def test_backward_compatibility(self):
7977
"""Test that all existing functionality remains intact."""
8078
if not os.path.exists(self.test_docx_path):
8179
pytest.skip("Test DOCX file not found")
82-
80+
8381
# Test different ways of calling convert
8482
result1 = self.markitdown.convert(self.test_docx_path)
8583
result2 = self.markitdown.convert(self.test_docx_path, extract_pages=False)
8684
result3 = self.markitdown.convert(self.test_docx_path, extract_pages=True)
87-
85+
8886
# Results should be equivalent for markdown content
8987
assert result1.markdown == result2.markdown
9088
assert result1.markdown == result3.markdown
9189
assert result1.title == result2.title
9290
assert result1.title == result3.title
93-
91+
9492
# All should have None for pages (DOCX limitation)
9593
assert result1.pages is None
9694
assert result2.pages is None
9795
assert result3.pages is None
98-
96+
9997
# All should work with string conversion
10098
assert str(result1) == str(result2)
10199
assert str(result1) == str(result3)
102-
100+
103101
# All should work with text_content property
104102
assert result1.text_content == result2.text_content
105103
assert result1.text_content == result3.text_content
106-
104+
107105
def test_extract_pages_parameter_types(self):
108106
"""Test different types for extract_pages parameter."""
109107
if not os.path.exists(self.test_docx_path):
110108
pytest.skip("Test DOCX file not found")
111-
109+
112110
# Test with different truthy/falsy values
113111
result_false = self.markitdown.convert(self.test_docx_path, extract_pages=False)
114112
result_true = self.markitdown.convert(self.test_docx_path, extract_pages=True)
115113
result_none = self.markitdown.convert(self.test_docx_path, extract_pages=None)
116114
result_zero = self.markitdown.convert(self.test_docx_path, extract_pages=0)
117115
result_one = self.markitdown.convert(self.test_docx_path, extract_pages=1)
118-
116+
119117
# All should return None for pages (DOCX limitation)
120118
assert result_false.pages is None
121119
assert result_true.pages is None
122120
assert result_none.pages is None
123121
assert result_zero.pages is None
124-
assert result_one.pages is None
122+
assert result_one.pages is None

0 commit comments

Comments
 (0)