Skip to content

Commit 451118d

Browse files
Create markitdown.py
1 parent 6febf58 commit 451118d

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

ai/markitdown.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#  Moon-Userbot - telegram userbot
2+
#  Copyright (C) 2020-present Moon Userbot Organization
3+
#
4+
#  This program is free software: you can redistribute it and/or modify
5+
#  it under the terms of the GNU General Public License as published by
6+
#  the Free Software Foundation, either version 3 of the License, or
7+
#  (at your option) any later version.
8+
9+
#  This program is distributed in the hope that it will be useful,
10+
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12+
#  GNU General Public License for more details.
13+
14+
#  You should have received a copy of the GNU General Public License
15+
#  along with this program.  If not, see <https://www.gnu.org/licenses/>.
16+
17+
import os
18+
from pyrogram import Client, filters
19+
from pyrogram.types import Message
20+
21+
from utils.misc import modules_help
22+
from utils.scripts import prefix, import_library, with_reply
23+
24+
import_library("markitdown")
25+
26+
from markitdown import MarkItDown
27+
28+
29+
@Client.on_message(filters.command("markitdown", prefix) & filters.me)
30+
@with_reply
31+
async def markitdown(client: Client, message: Message):
32+
if message.reply_to_message.document:
33+
await message.edit("Converting to Markdown...")
34+
file = await message.reply_to_message.download()
35+
file_name = (message.reply_to_message.document.file_name).split(".")[0] + ".md"
36+
markitdown = MarkItDown()
37+
result = markitdown.convert(file)
38+
with open(file_name, "w") as f:
39+
f.write(result.text_content)
40+
await message.edit("Uploading...")
41+
await client.send_document(
42+
message.chat.id, file_name, reply_to_message_id=message.reply_to_message.id
43+
)
44+
os.remove(file)
45+
os.remove(file_name)
46+
await message.delete()
47+
else:
48+
await message.edit("Reply to a document to convert it to Markdown.")
49+
50+
51+
modules_help["markitdown"] = {"markitdown": "Convert a document to Markdown."}

0 commit comments

Comments
 (0)