This Python code is for a Telegram bot that can convert documents to PDF format. Here’s a simple summary:
1. The code imports necessary libraries for the Telegram bot, handling document conversions, and working with Excel and PowerPoint files.
2. You need to replace 'YOUR_API_KEY'
with your actual Telegram bot API key.
3. The bot responds to the /start
command by sending a greeting message.
4. When the user sends a document (e.g., Word, Excel, PowerPoint) to the bot, it triggers the handle_document
function.
5. The bot downloads the document, saves it with the original file name, and determines its file extension.
6. Depending on the file type (.docx
, .xls
, .xlsx
, .ppt
, .pptx
), the bot uses the appropriate library to convert the document to PDF.
7. The converted PDF is sent back to the user.
8. The bot cleans up by deleting the downloaded and converted files after sending the PDF.
9. If any error occurs during the process, the bot sends an error message to the user.
To use this code, you should have the required libraries installed and a valid Telegram bot API key. The bot should work as long as the code is running and the API key is valid.
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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
import telebot import os import docx2pdf # Library for converting .doc files to PDF import comtypes.client # Library for converting Excel and PowerPoint files to PDF # Replace 'YOUR_API_KEY' with your actual API key API_KEY = 'YOUR_API_KEY' bot = telebot.TeleBot(API_KEY) # Handler for the /start command @bot.message_handler(commands=['start']) def start(message): bot.send_message(message.chat.id, "Hello! I am your bot. Send me a document, and I'll convert it to PDF for you.") # Handler for document messages @bot.message_handler(content_types=['document']) def handle_document(message): try: # Download the document file_info = bot.get_file(message.document.file_id) downloaded_file = bot.download_file(file_info.file_path) # Save the document with the original file name file_name = message.document.file_name with open(file_name, 'wb') as new_file: new_file.write(downloaded_file) # Determine the file extension _, file_extension = os.path.splitext(file_name) # Convert the document to PDF based on the file extension pdf_file_name = file_name + '.pdf' if file_extension == '.docx': docx2pdf.convert(file_name, pdf_file_name) elif file_extension in ['.xls', '.xlsx', '.ppt', '.pptx']: convert_to_pdf(file_name, pdf_file_name) # Send the converted PDF back to the user with open(pdf_file_name, 'rb') as pdf_file: bot.send_document(message.chat.id, pdf_file) # Clean up: delete the downloaded and converted files os.remove(file_name) os.remove(pdf_file_name) except Exception as e: bot.send_message(message.chat.id, "Oops! Something went wrong. Please try again later.") def convert_to_pdf(input_file, output_file): # Function to convert Excel and PowerPoint files to PDF using pywin32 # Create COM object excel = comtypes.client.CreateObject("Excel.Application") ppt = comtypes.client.CreateObject("PowerPoint.Application") try: # Open the input file if input_file.lower().endswith(('.xls', '.xlsx')): workbook = excel.Workbooks.Open(os.path.abspath(input_file)) workbook.ExportAsFixedFormat(0, os.path.abspath(output_file)) excel.Quit() elif input_file.lower().endswith(('.ppt', '.pptx')): presentation = ppt.Presentations.Open(os.path.abspath(input_file)) presentation.SaveAs(os.path.abspath(output_file), 32) # 32 is the value for PDF format ppt.Quit() else: raise ValueError("Unsupported file format") except Exception as e: excel.Quit() ppt.Quit() raise e if __name__ == "__main__": bot.polling() |
Now the bot should handle Word, Excel, and PowerPoint files and convert them to PDF accordingly. Please replace 'YOUR_API_KEY'
with your actual API key to make it work.