Skip to content

Commit 563a2d6

Browse files
CopilotKSemenenko
andcommitted
Fix remaining converters: Remove unnecessary Task.Run from DocxConverter, XlsxConverter, and PptxConverter
Co-authored-by: KSemenenko <4385716+KSemenenko@users.noreply.github.com>
1 parent 6e0f290 commit 563a2d6

3 files changed

Lines changed: 33 additions & 42 deletions

File tree

src/MarkItDown/Converters/DocxConverter.cs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -89,22 +89,19 @@ public async Task<DocumentConverterResult> ConvertAsync(Stream stream, StreamInf
8989
}
9090
}
9191

92-
private static async Task<string> ExtractTextFromDocxAsync(Stream stream, CancellationToken cancellationToken)
92+
private static Task<string> ExtractTextFromDocxAsync(Stream stream, CancellationToken cancellationToken)
9393
{
9494
var result = new StringBuilder();
9595

96-
await Task.Run(() =>
97-
{
98-
using var wordDocument = WordprocessingDocument.Open(stream, false);
99-
var body = wordDocument.MainDocumentPart?.Document?.Body;
96+
using var wordDocument = WordprocessingDocument.Open(stream, false);
97+
var body = wordDocument.MainDocumentPart?.Document?.Body;
10098

101-
if (body != null)
102-
{
103-
ProcessBodyElements(body, result, cancellationToken);
104-
}
105-
}, cancellationToken);
99+
if (body != null)
100+
{
101+
ProcessBodyElements(body, result, cancellationToken);
102+
}
106103

107-
return result.ToString().Trim();
104+
return Task.FromResult(result.ToString().Trim());
108105
}
109106

110107
private static void ProcessBodyElements(Body body, StringBuilder result, CancellationToken cancellationToken)

src/MarkItDown/Converters/PptxConverter.cs

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -91,31 +91,28 @@ public async Task<DocumentConverterResult> ConvertAsync(Stream stream, StreamInf
9191
}
9292
}
9393

94-
private static async Task<string> ExtractContentFromPptxAsync(Stream stream, CancellationToken cancellationToken)
94+
private static Task<string> ExtractContentFromPptxAsync(Stream stream, CancellationToken cancellationToken)
9595
{
9696
var result = new StringBuilder();
9797

98-
await Task.Run(() =>
98+
using var presentationDocument = PresentationDocument.Open(stream, false);
99+
var presentationPart = presentationDocument.PresentationPart;
100+
101+
if (presentationPart?.Presentation?.SlideIdList != null)
99102
{
100-
using var presentationDocument = PresentationDocument.Open(stream, false);
101-
var presentationPart = presentationDocument.PresentationPart;
103+
var slideCount = 0;
102104

103-
if (presentationPart?.Presentation?.SlideIdList != null)
105+
foreach (var slideId in presentationPart.Presentation.SlideIdList.Elements<SlideId>())
104106
{
105-
var slideCount = 0;
107+
cancellationToken.ThrowIfCancellationRequested();
106108

107-
foreach (var slideId in presentationPart.Presentation.SlideIdList.Elements<SlideId>())
108-
{
109-
cancellationToken.ThrowIfCancellationRequested();
110-
111-
slideCount++;
112-
var slidePart = (SlidePart)presentationPart.GetPartById(slideId.RelationshipId!);
113-
ProcessSlide(slidePart, slideCount, result);
114-
}
109+
slideCount++;
110+
var slidePart = (SlidePart)presentationPart.GetPartById(slideId.RelationshipId!);
111+
ProcessSlide(slidePart, slideCount, result);
115112
}
116-
}, cancellationToken);
113+
}
117114

118-
return result.ToString().Trim();
115+
return Task.FromResult(result.ToString().Trim());
119116
}
120117

121118
private static void ProcessSlide(SlidePart slidePart, int slideNumber, StringBuilder result)

src/MarkItDown/Converters/XlsxConverter.cs

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -89,28 +89,25 @@ public async Task<DocumentConverterResult> ConvertAsync(Stream stream, StreamInf
8989
}
9090
}
9191

92-
private static async Task<string> ExtractDataFromXlsxAsync(Stream stream, CancellationToken cancellationToken)
92+
private static Task<string> ExtractDataFromXlsxAsync(Stream stream, CancellationToken cancellationToken)
9393
{
9494
var result = new StringBuilder();
9595

96-
await Task.Run(() =>
96+
using var spreadsheetDocument = SpreadsheetDocument.Open(stream, false);
97+
var workbookPart = spreadsheetDocument.WorkbookPart;
98+
99+
if (workbookPart?.Workbook?.Sheets != null)
97100
{
98-
using var spreadsheetDocument = SpreadsheetDocument.Open(stream, false);
99-
var workbookPart = spreadsheetDocument.WorkbookPart;
100-
101-
if (workbookPart?.Workbook?.Sheets != null)
101+
foreach (var sheet in workbookPart.Workbook.Sheets.Elements<Sheet>())
102102
{
103-
foreach (var sheet in workbookPart.Workbook.Sheets.Elements<Sheet>())
104-
{
105-
cancellationToken.ThrowIfCancellationRequested();
106-
107-
var worksheetPart = (WorksheetPart)workbookPart.GetPartById(sheet.Id!);
108-
ProcessWorksheet(worksheetPart, sheet.Name?.Value ?? "Sheet", result, workbookPart);
109-
}
103+
cancellationToken.ThrowIfCancellationRequested();
104+
105+
var worksheetPart = (WorksheetPart)workbookPart.GetPartById(sheet.Id!);
106+
ProcessWorksheet(worksheetPart, sheet.Name?.Value ?? "Sheet", result, workbookPart);
110107
}
111-
}, cancellationToken);
108+
}
112109

113-
return result.ToString().Trim();
110+
return Task.FromResult(result.ToString().Trim());
114111
}
115112

116113
private static void ProcessWorksheet(WorksheetPart worksheetPart, string sheetName, StringBuilder result, WorkbookPart workbookPart)

0 commit comments

Comments
 (0)