-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
54 lines (41 loc) · 1.7 KB
/
main.py
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
import logging
import os
from telegram.ext import Updater, CommandHandler
import pypdf2
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
logger = logging.getLogger(__name__)
def start(update, context):
update.message.reply_text("Hello! I'm a bot that can edit PDF files. Send me a PDF file and a command to get started.")
def edit_pdf(update, context):
# Get the file_id of the PDF that the user sent
file_id = update.message.document.file_id
file = context.bot.get_file(file_id)
file.download('file.pdf')
# Open the PDF using the PyPDF2 library
with open('file.pdf', 'rb') as f:
pdf = pypdf2.PdfFileReader(f)
# Edit the PDF as desired using the PyPDF2 library
# For example, you could add a watermark to all pages of the PDF
watermark = pypdf2.PdfFileReader(open('watermark.pdf', 'rb'))
for page in range(pdf.getNumPages()):
pdf.getPage(page).mergePage(watermark.getPage(0))
# Save the edited PDF to a new file
with open('edited_file.pdf', 'wb') as f:
pdf.write(f)
# Send the edited PDF back to the user
with open('edited_file.pdf', 'rb') as f:
context.bot.send_document(chat_id=update.effective_chat.id, document=f)
# Delete the temporary files
os.remove('file.pdf')
os.remove('edited_file.pdf')
def main():
# Set up the Updater and add the command handlers
updater = Updater("TOKEN", use_context=True)
dp = updater.dispatcher
dp.add_handler(CommandHandler("start", start))
dp.add_handler(CommandHandler("edit", edit_pdf))
# Start the bot
updater.start_polling()
updater.idle()
if __name__ == '__main__':
main()