【pywpsrpc/Python】改变图片宽高报错 对象无效属性: ‘tuple‘ object has no attribute ‘Width‘

概要

开发UOS环境下,使用Python通过pywpsrpc库操作word文档在指定段落添加指定宽高的图片时意外发现的该问题,在原作者的github提问后获得解答后记录(源码地址、对应提问链接以及相关资料链接贴在文章末尾)。

原因及解决方式

首先贴一段简单的代码,作用是新建一个word文档并在其中添加一个图片。

from pywpsrpc.rpcwpsapi import createWpsRpcInstance

hr, rpc = createWpsRpcInstance()
hr, app = rpc.getWpsApplication()

hr, doc = app.Documents.Add()
parag = doc.Paragraphs.Add().Range
img = parag.InlineShapes.AddPictures('/path/img.jpg')

可以看到在最后我们获得了一个InlineShape对象img,如果我们这时候需要对这个图片进行操作,按照wps官方文档,我们应该只需直接通过:

width = 100
height = 100
img.Width = width
img.Height = height

即可完成自定义该图片的长宽,但是pywpsrpc返回的却是默认的tuple所以引发了标题中的报错,此时只需要调用RpcProxy将该对象转换即可,以下是修改后的代码:

from pywpsrpc.rpcwpsapi import createWpsRpcInstance
from pywpsrpc import RpcProxy

hr, rpc = createWpsRpcInstance()
hr, app = rpc.getWpsApplication()

hr, doc = app.Documents.Add()
parag = doc.Paragraphs.Add().Range
img = parag.InlineShapes.AddPictures('/path/img.jpg')
width = 100
height = 100
RpcProxy(img).Width = width
RpcProxy(img).Height = height

doc.SaveAs2('/path/word.docx')
app.Quit()

问题解决,以上。

相关链接

pywpsrpc库:https://github.com/timxx/pywpsrpc
提问链接:https://github.com/timxx/pywpsrpc/issues/93
RpcProxy wiki:https://github.com/timxx/pywpsrpc/wiki/RpcProxy

  • 8
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python编程中,当你看到类似于"AttributeError: 'tuple' object has no attribute 'xxx'"的错误消息时,这意味着你在一个元组对象上尝试调用一个不存在的属性。元组是Python中的不可变序列类型,不支持修改元素的操作或添加新属性。 在引用中,报错是因为尝试对一个元组对象调用encode方法,但元组对象没有encode属性。这通常发生在尝试发送邮件时。 在引用中,报错是因为尝试对一个元组对象调用shape属性,但元组对象没有shape属性。这通常发生在尝试对图像进行resize操作时。 在引用中,报错是因为安装了与Python版本不兼容的包,导致出现冲突。具体来说,某个包的接口与Python 3.5版本严重冲突,而你使用的是Python 3.7版本。 为了解决这些问题,你可以检查你的代码,确认是否使用了正确的对象类型。如果是在调用第三方库时出现了问题,你可以尝试更新库的版本或寻找其他解决方案。如果是版本兼容性问题,你可以尝试降级Python版本或找到适配当前Python版本的包。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [python编程,排除163邮箱发送邮件报错AttributeError: ‘tupleobject has no attribute ‘encode‘)](https://blog.csdn.net/qq_40674586/article/details/107056128)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [使用VideoCapture().read()时,imutils报错AttributeError: ‘tupleobject has no attribute ...](https://blog.csdn.net/weixin_48911487/article/details/123828104)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [pip install xxx 出现 AttributeError: ‘tupleobject has no attribute ‘read’ 错误](https://blog.csdn.net/weixin_48636525/article/details/126162274)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值