-
Notifications
You must be signed in to change notification settings - Fork 0
/
markitdown-deno.ts
60 lines (43 loc) · 1.55 KB
/
markitdown-deno.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import { loadPyodide } from 'npm:pyodide';
import { join as joinPaths, SEPARATOR as SEP } from "jsr:@std/path";
const INPUT_FILE = 'example.xlsx';
const OUTPUT_FILE = INPUT_FILE + '.md';
const INPUT_PATH = joinPaths(Deno.cwd(), INPUT_FILE);
const OUTPUT_PATH = joinPaths(Deno.cwd(), OUTPUT_FILE);
// Load and set up Pyodide
//
//
const pyodide = await loadPyodide();
await pyodide.loadPackage(['micropip']);
// Run conversion
//
//
convertToMarkdown(INPUT_PATH, OUTPUT_PATH);
/**
*
*
*
*/
async function convertToMarkdown(inputPath: string, outputPath: string): Promise<void> {
// Read the data from the filesystem
const inputData = Deno.readFileSync(inputPath);
// Set up and write the input to Pyodide's virtual filesystem
const inputFileName = inputPath.split(SEP).pop() as string;
const outputFileName = outputPath.split(SEP).pop() as string;
const virtualInput = '/' + inputFileName;
const virtualOutput = '/' + outputFileName;
pyodide.FS.writeFile(virtualInput, inputData);
// Execute the Python code
const pythonCode = `
import micropip
await micropip.install("markitdown")
from markitdown import MarkItDown
markitdown = MarkItDown()
markitdown.convert(source="${virtualInput}", output_path="${virtualOutput}")
`;
await pyodide.runPythonAsync(pythonCode);
// Retrieve and save the output data
const outputData = pyodide.FS.readFile(virtualOutput);
Deno.writeFileSync(outputPath, outputData);
console.log(`Output written to ${outputPath}`);
}