简体   繁体   中英

Add watermark to word document using Python

I want to add an image as background/watermark to a new word document using Python. I tried Python-docx but couldn't find anything useful

from docx import Document
from docx.shared import Inches

document = Document()

document.add_picture(r'D:\Python\Projects\raw_imgs\3b057d6199d95c4339ef532001cb20cd.jpg', width=Inches(6))
document.save('demo.docx')

The above code just inserts the image but I want to add it as the background image.

Aspose.Words Cloud SDK for Python can insert an image as a background to the DOC/DOCX. Though it is paid product, its free trial allows 150 free API calls monthly.

# For complete examples and data files, please go to https://github.com/aspose-words-cloud/aspose-words-cloud-python
# Import module
import asposewordscloud
import asposewordscloud.models.requests
from shutil import copyfile

# Please get your Client ID and Secret from https://dashboard.aspose.cloud.
client_id='xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx'
client_secret='xxxxxxxxxxxxxxxxxxxxxxxxxxx'

words_api = asposewordscloud.WordsApi(client_id,client_secret)
words_api.api_client.configuration.host='https://api.aspose.cloud'

localFile = 'C:/Temp/Sections.docx'
imageFile= 'C:/Temp/Tulips.jpg'
outputFile= 'C:/Temp/Watermark.docx'

request = asposewordscloud.models.requests.InsertWatermarkImageOnlineRequest(document=open(localFile, 'rb'), image_file=open(imageFile, 'rb'))

result = words_api.insert_watermark_image_online(request)
copyfile(result.document, outputFile)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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