繁体   English   中英

python 中的 Wordpress_xmlrpc 图像 alt 属性

[英]Wordpress_xmlrpc image alt attribute in python

我尝试使用 XML-RPC python 在 WordPress 中上传图像。 但是在将图像上传到 wordpress 时,我无法为图像设置图像 alt 属性。 我该如何添加它?

这是我在python 3.7中的 wordpress 中上传图像的代码:

# for wordpress
from wordpress_xmlrpc import Client, WordPressPost
from wordpress_xmlrpc.methods.posts import NewPost, GetPosts, GetPost, EditPost
from wordpress_xmlrpc.methods import media
from wordpress_xmlrpc.compat import xmlrpc_client


class WP_CLIENT():
    def __init__(self, url, username, passwd):
        self.url = url
        self.username = username
        self.passwd = passwd
        self.client = Client(url, username, passwd)

    # upload image
    def upload_image(self, path, name):
        # get the file type
        file_type = 'image/'+path.split('.')[-1].lower()
        # change jpg to jpeg
        file_type = file_type.replace('jpg', 'jpeg')

        # prepare metadata
        data = {
            'name': name,
            'type': file_type,
        }


    # read the binary file and let the XMLRPC library encode it into base64
    with open(path, 'rb') as img:
        data['bits'] = xmlrpc_client.Binary(img.read())

    response = self.client.call(media.UploadFile(data))
    # for referance purpose
    # response == {
    #       'id': 6,
    #       'file': 'picture.jpg'
    #       'url': 'http://www.example.com/wp-content/uploads/2012/04/16/picture.jpg',
    #       'type': 'image/jpeg',
    # }

    return response

WordPress上传图片时如何添加图片alt属性?

提前致谢

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM