Python Forum
Copy data from Excel and paste into Discord (Midjourney)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Copy data from Excel and paste into Discord (Midjourney)
#1
Task. A program for copying data from an Excel table and pasting it into the Discord command line (Midjourney), using the openpyxl library for working with Excel.

Problem.
Not copied from Excel

It gives the following error.
Discord 1.0.9013
Exit from the secondary instance.

**************
import subprocess
import time
from openpyxl import load_workbook

# Opening an Excel file
workbook = load_workbook('c:\AutomationMJ\copyExcelTOmj.xlsx')

# Selecting a letter in an Excel table
worksheet = workbook['Sheet1']

# List of cells from which to copy data
cells_to_copy = ['A1', 'A2', 'A3']

# Loop for each cell
for cell_reference in cells_to_copy:
    # Get the value from the selected cell
    cell_value = worksheet[cell_reference].value

    # Opening Discord in the background and passing a value via standard input
    command = ['C:\\Users\\irave\\AppData\\Local\\Discord\\app-1.0.9013\\Discord.exe', '/imagine', ' ' + str(cell_value)]
    process = subprocess.Popen(command, stdin=subprocess.PIPE)
    process.communicate()

    # Delay of 10 seconds before opening the next Discord process
    time.sleep(10)
buran write Jun-01-2023, 08:39 AM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply
#2
Dos it work outside of the loop also with one vaule?
Eg add print(cell_value) on line 18.

Test like this.
import subprocess

cell_vaule =  "A dog in space"
command = ['C:\\Users\\irave\\AppData\\Local\\Discord\\app-1.0.9013\\Discord.exe', '/imagine', ' ' + str(cell_value)]
process = subprocess.Popen(command, stdin=subprocess.PIPE)
process.communicate()
Or with run()
import subprocess
 
cell_vaule =  "A dog in space"
command = ['C:\\Users\\irave\\AppData\\Local\\Discord\\app-1.0.9013\\Discord.exe', '/imagine', ' ' + str(cell_value)]
out = subprocess.run(command , capture_output=True)
print(out.stdout.decode())
I use Midjourney and it return a Image format .png,do you have command from command line that work and give you raw .png format back?
Reply
#3
(Jun-01-2023, 11:42 AM)snippsat Wrote: Dos it work outside of the loop also with one vaule?
Eg add print(cell_value) on line 18.

Test like this.
[python]import subprocess

thanks for the help.
But none of these options work. So the problem is different.

(Jun-01-2023, 11:42 AM)snippsat Wrote: I use Midjourney and it return a Image format .png,do you have command from command line that work and give you raw .png format back?

All png files generated in MJ can be downloaded in one archive. there is no need to copy each file separately.
Reply
#4
I have not look if Midjourney has an Api,so if search How to Use the Missing Midjourney API.
Quote:If you want to create images using Midjourney, you’re stuck using Discord.
Sort of.

Just writing a bog-standard Discord bot won’t work because the Midjourney bot expects “/imagine” and then “tab” and then your prompt. It’s tedious.

Unfortunately, since it’s a fake gui operator waiting for events,
I haven’t quite got it modified to create those database entries.
In theory it should be possible to get the attachments and save them – work in progress!

So there is an Api but:
Quote:The API is stopping it’s operation but you can purchase it’s source code.
Joe_Wright likes this post
Reply
#5
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.
snippsat write Jun-06-2023, 06:33 PM:
Added code tag in your post,look at BBCode on how to use.
Joe_Wright likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Why is the copy method name in python list copy and not `__copy__`? YouHoGeon 2 286 Apr-04-2024, 01:18 AM
Last Post: YouHoGeon
  Copy Paste excel files based on the first letters of the file name Viento 2 455 Feb-07-2024, 12:24 PM
Last Post: Viento
  What script to paste folders thenewcoder 1 677 Nov-29-2023, 09:40 AM
Last Post: Pedroski55
  How to copy work sheet data one workbook to other? sayyedkamran 2 709 Nov-03-2023, 09:10 AM
Last Post: Larz60+
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,116 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  How to properly format rows and columns in excel data from parsed .txt blocks jh67 7 1,890 Dec-12-2022, 08:22 PM
Last Post: jh67
  Trying to Get Arduino sensor data over to excel using Python. eh5713 1 1,725 Dec-01-2022, 01:52 PM
Last Post: deanhystad
  is it possible to copy image from email and place into excel file? cubangt 3 1,282 Nov-30-2022, 05:11 PM
Last Post: snippsat
  Please help me [copy and paste file from src to dst] midomarc 2 1,027 Nov-24-2022, 10:13 PM
Last Post: midomarc
  Appending a row of data in an MS Excel file azizrasul 3 1,197 Nov-06-2022, 05:17 PM
Last Post: azizrasul

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020