Jun-06-2023, 05:49 PM
(This post was last modified: Jun-06-2023, 06:33 PM by snippsat.
Edit Reason: Added code tag
)
import discord from discord.ext import commands from openpyxl import load_workbook # Discord Bot Token TOKEN = 'YOUR_DISCORD_BOT_TOKEN' # Excel File Path EXCEL_FILE = 'path_to_your_excel_file.xlsx' # Discord Channel ID CHANNEL_ID = 'your_discord_channel_id' # Initialize the Discord Bot bot = commands.Bot(command_prefix='!') # Event: When the bot is ready @bot.event async def on_ready(): print(f'Logged in as {bot.user.name}') print('-----') # Custom Command: Copy and Paste Excel Data to Discord @bot.command() async def copy_paste_excel(ctx): # Load the Excel workbook workbook = load_workbook(EXCEL_FILE) # Select the active sheet sheet = workbook.active # Iterate over the rows in the Excel table for row in sheet.iter_rows(min_row=2, values_only=True): # Format the row data as a string row_data = ' '.join(str(cell) for cell in row if cell is not None) # Send the row data to the Discord channel channel = bot.get_channel(CHANNEL_ID) await channel.send(row_data) await ctx.send('Excel data copied and pasted to Discord.') # Run the Discord bot bot.run(TOKEN)-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Make sure to replace 'YOUR_DISCORD_BOT_TOKEN' with your Discord bot token, 'path_to_your_excel_file.xlsx' with the actual path to your Excel file, and 'your_discord_channel_id' with the ID of the Discord channel where you want to send the data.
To use the bot, you can invite it to your server and use the command !copy_paste_excel to copy the data from the Excel table and paste it into the specified Discord channel.
Please note that you need to have the necessary dependencies (discord.py, openpyxl) installed for this script to work. You can install them using pip install discord.py openpyxl.