Upload a large number of images to Wordpress

I needed to upload a lot of images (10,000 or more) to wordpress, and I tried to use "Media UP FTP", but I got an error probably because there were too many. In the first place, it took too long to load and I couldn't upload properly. Therefore, I decided to operate the browser with python and upload the image to Wordpress like RPA. Affinger5 is used for the Wordpress theme. I haven't used any other theme, so I don't know if it works in other environments. The browser used is Chrome.


from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
from selenium.webdriver.common.keys import Keys
import glob
import os

#Function to temporarily stop browser operation
def sleep(sleep_time):
    time.sleep(sleep_time)

#Log in to Wordpress.
driver = webdriver.Chrome()
wordpress_url = "URL of my Wordpress"
driver.get(wordpress_url)
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "wp-submit")))
# ID/Enter PASS
sleep(0.1)
ID = driver.find_element_by_id("user_login")
ID.send_keys("ID to log in")
sleep(0.1)
password = driver.find_element_by_id("user_pass")
password.send_keys("Password to log in")
sleep(0.1)
#Click the login button
login_button = driver.find_element_by_name("wp-submit")
login_button.click()
sleep(0.1)

#Move to the posting screen
driver.get("URL of the posting screen")
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "footer-upgrade")))
sleep(0.1)

#Go to media upload
driver.get("URL")

#Specify the folder where the images are saved
#Specify only JPG this time
imgs = glob.glob(r"C:\img\*.jpg ")

#Code to upload image
#Before uploading an image, you have to upload the image yourself by the same procedure
#Since you operate Explorer directly, when you press the "Select File" button, the folder you want to upload must be open.
for img in imgs:
    
    media = driver.find_element_by_id("plupload-browse-button")
    sleep(0.1)
    media.click()
    sleep(0.3)

    #The name of the image is the same as the original image
    img_name = os.path.basename(img)

    #Copy and paste of image name
    pyperclip.copy(img_name)
    pgui.hotkey('ctrl', 'v')
    sleep(0.3)

    #Press ENTER to select an image and close Explorer.
    pgui.typewrite(['enter'])
    sleep(0.1)    

You will not be able to use your computer while the code is running, because you are just letting your computer do your work. Also, you will need the Chrome Driver to use the webdriver, so please download the one that corresponds to the version of Chrome you are using. Perhaps you also need to set environment variables. I think other people have explained it in detail, so please look for it.

Recommended Posts

Upload a large number of images to Wordpress
TensorFlow To learn from a large number of images ... ~ (almost) solution ~
TensorFlow To learn from a large number of images ... (Unsolved problem) → 12/18 Solved
Connect a large number of videos together!
Convert a large number of PDF files to text files using pdfminer
Organize a large number of files into folders
I wrote a script to upload a WordPress plugin
I want to solve the problem of memory leak when outputting a large number of images with Matplotlib
Create a dataset of images to use for learning
Accelerate a large number of simple queries with MySQL
[Python] Randomly generate a large number of English names
Upload a file to Dropbox
A tool to follow posters with a large number of likes on instagram [25 minutes to 1 second]
Use API to mark a large number of unread emails in Gmail as read
Paste a large number of image files into PowerPoint [python-pptx]
How to increase the number of machine learning dataset images
Scrapy-Redis is recommended for crawling a large number of domains
Serial number rename of scraped images
Executing a large number of Python3 Executor.submit may consume a lot of memory
How to create a large amount of test data in MySQL? ??
[Django] What to do if the model you want to create has a large number of fields
Maximum average number of daily visitors (large)
Use shutil to delete all folders with a small number of files
ETL processing for a large number of GTFS Realtime files (Python edition)
Try to make a kernel of Jupyter
[Python] A program that calculates the number of socks to be paired
[Python] How to put any number of standard inputs in a list
How to put a line number at the beginning of a CSV file
I want to backtest a large number of exchange pairs and strategies at once with Python's backtesting.py
How to play a video while watching the number of frames (Mac)
Summary of how to build a LAMP + Wordpress environment with Sakura VPS
Upload images to Google Drive with Python
Let Code Day6 Starting from Zero "1342. Number of Steps to Reduce a Number to Zero"
Set the number of elements in a NumPy one-dimensional array to a power of 2 (0 padded)
Find out how to divide a file with a certain number of lines evenly
How to count the number of elements in Django and output to a template
Use twitter API to get the number of tweets related to a certain keyword
How to calculate the volatility of a brand
A simple example of how to use ArgumentParser
Convert data with shape (number of data, 1) to (number of data,) with numpy.
Convert "number" of excel date to python datetime
Find the number of days in a month
Anonymous upload of images using Imgur API (using Python)
Upload images to S3 with GUI using tkinter
How to upload to a shared drive using pydrive
I want to upload a Django app to heroku
How to identify the element with the smallest number of characters in a Python list?
Consolidate a large number of CSV files in folders with python (data without header)
I want to collect a lot of images, so I tried using "google image download"
A script that can perform stress tests according to the number of CPU cores
Find a guideline for the number of processes / threads to set in the application server
python beginners tried to predict the number of criminals
How to know the port number of the xinetd service
I want to detect images of cats from Instagram
A memo connected to HiveServer2 of EMR with python
How to get the number of digits in Python
Convert a slice object to a list of index numbers
A memo of how to use AIST supercomputer ABCI
A list of stumbling blocks in Django's image upload
A memo to visually understand the axis of pandas.Panel
How to write a list / dictionary type of Python3