Here’s the complete code with the necessary corrections, explanations, and potential enhancements.

import datetime, xmlrpc.client as xmlrpclib, csv

# STEP 1: WordPress Connection Details
# Replace with your actual WordPress credentials and settings
wp_url = "https://exam.pscnotes.com/xmlrpc.php"
wp_username = "rawan239"
wp_password = "9412274025"
wp_blogid = ""  # Replace with your blog ID if necessary
postype = ""  # Set the desired post type (e.g., 'post')

# STEP 2:  WordPress Post Insertion Function 
def insert_wp_post(title="Title was not passed !", content="Content was not passed !"):
    server = xmlrpclib.ServerProxy(wp_url)
    date_created = xmlrpclib.DateTime(datetime.datetime.strptime("2024-02-21 21:08", "%Y-%m-%d %H:%M"))
    categories = ["unsorted"]
    tags = ["sometag", "othertag"]
    data = {
        'post_type': postype,
        'title': title, 
        'description': content,
        'dateCreated': date_created,
        'categories': categories,
        'mt_keywords': tags
    }

    try:
        post_id = server.wp.newPost(wp_blogid, wp_username, wp_password, data, status_published=0)  # Change status if needed
        print(f"Post created with ID: {post_id}")
    except xmlrpclib.Fault as e:
        print(f"Error communicating with WordPress: {e}")

# STEP 3: Read CSV and Insert Posts
try:
    reader = csv.reader(open("indiaafterindependence-output.csv", "r", encoding='utf-8'))
    for row in reader:
        insert_wp_post(row[1], row[2])  # Assuming title in column 1, content in column 2
except csv.Error as e:
    print(f"Error reading CSV file: {e}")

How to Use:

  1. Credentials: Fill in the wp_urlwp_usernamewp_password, and wp_blogid with your WordPress website’s information.
  2. Settings: Set the desired postype if you want something other than the default ‘post’.
  3. CSV File: Ensure you have a file named “indiaafterindependence-output.csv” in the same directory as this script. Adapt the file name if different.
  4. Run the Script: Execute this Python script (e.g., python wpadapter.py).

Important Notes:

  • CSV Structure: The code assumes your CSV file has the title in the second column (index 1) and the content in the third column (index 2). Adjust the indexes if your CSV has a different structure.
  • WordPress API: The script uses the XML-RPC API from WordPress. Make sure it’s enabled on your WordPress installation.

Let me know if you have any specific customizations or want help with further refinements!