最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to editchange every hyperlink in a word document with python-docx or some other library? - Stack Overflow

programmeradmin0浏览0评论

I have been trying to re-link a .docx file that contains many hyperlinks embedded inside text sections since we decided to change document storage services. So the links on the docx are old and need to be swapped out with the new updated links. I am able to access all the old links using the python-docx library and have saved them in a csv, but I can't figure out a way to edit/swap out the old hyperlinks with new ones.

I have tried doing hyperlink.address = "www.google" expecting to be able to change the embedded address. But that threw the error AttributeError: property 'address' of 'Hyperlink' object has no setter Even when I try replacing the entire link object, nothing seems to happen even after I save the document and the document would be the same as the old document.

counter = 0

for table in document.tables:
    for row in table.rows:
        for cell in row.cells:
            for paragraph in cell.paragraphs:
                hyperlinks = paragraph.hyperlinks
                for link in hyperlinks:
                    link = "www.google"
                    
document.save('TABLETEST2.docx')

I would love help on how to approach this problem either using python-docx or some other library/tool, Thank you!

发布评论

评论列表(0)

  1. 暂无评论