Home » Scripts » Python Script to Bulk Upload new WordPress Posts from a CSV file using WordPress Rest API

Python Script to Bulk Upload new WordPress Posts from a CSV file using WordPress Rest API

Python Script to Bulk Upload new WordPress Posts from a CSV file using WordPress Rest API. Learn how to quickly upload new WordPress posts in bulk using the WordPress API. This tutorial will show you how to connect to the WordPress Rest API using Python and Google Colab.

Contents
1, Create a WordPress Application Password
2, Create a new Colab document and import the required Python modules.
3, Connecting to the WordPress Rest API
4, Upload CSV and remote files directly into Google Colab
5, Fetching the CSV and pasting filename into the script
6, Creating a function that builds the JSON Posts array
7, Executing the script and monitoring the progress
8, Colab link to the full script and demo of expected results

WordPress thankfully has a fantastic fully functional Rest API that is surprisingly easy to use. The WordPress REST API is an interface that you can use to access WordPress from outside the WordPress installation itself. Using NodeJS or Python you can connect to the API to speed up productivity or use it for headless functionality.

If you are completely new to the WordPress Rest API I suggest visiting the developer docs to help you familiarise yourself with the API endpoint and functionality – https://developer.wordpress.com/docs/api/

To connect to WordPress I will be using Google Colab. Colab is a free Python Jupyter notebook environment in the cloud. Colab notebooks allow you to combine Python executable code and rich text within a single document.

We will be connecting with the WordPress API to bulk upload new posts from a CSV file. You can literally create thousands of new WordPress posts in a few minutes. This Python script is fantastic at saving time on your content production.

Below is a copy of the script if you don’t require the tutorial

# Import modules
import pandas as pd
import requests
import csv
import json
import base64
from tqdm.notebook import tqdm

# WordPress Rest API credentials
user = 'admin'
password = 'kJid Xqyu d7K9 fRmK 7cfN d8c6'
url = 'https://airwolf.dev/wp/wp-json/wp/v2'

wp_connection = user + ':' + password

token = base64.b64encode(wp_connection.encode())

# Base64 Authentication WordPress
headers = {'Authorization': 'Basic ' + token.decode('utf-8')}

from google.colab import files
uploaded = files.upload()

# Paste your uploaded csv file name here - example: sample-data.csv or paste remote web link
df = pd.read_csv("https://airwolf.dev/wp/sample_posts.csv")
df

# WP_Post_Insert Function to build the JSON array
# (add or remove post schemas to suit your requirements)
def wp_post_insert(post_title, post_content):

    post = {'title': post_title,
            'status': 'publish',
            'content': post_content,
            'author': '1',
            'format': 'standard'
            }

    wp_post_insert_request = requests.post(url + '/posts', headers=headers, json=post)

# Executes the script and runs the TQDM progress bar
for index, row in tqdm(df.iterrows()):
# Add or remove WordPress post schemas varibles below
    wp_post_insert(row["post_title"], row["post_content"])

Read the rest of the Tutorial here – https://chrisleverseo.com/t/python-script-to-bulk-upload-new-wordpress-posts-from-a-csv-file-using-wordpress-rest-api.93/