繁体   English   中英

使用python在docx文件中添加水印文本的方法是什么?

[英]What is the way to add watermark text in a docx file using python?

我正在使用似乎没有水印支持的 python-docx 模块操作 docx 文件。 使用 python 在 docx 文件中添加一些水印文本的可能方法是什么?

编辑:我创建了一个带有水印文本 = ' Tariq ' 的空白文档。 这包括 docx 文件的压缩版本中的“header1.xml”文件。

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:hdr mc:Ignorable="w14 wp14" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape">
    <w:p>
        <w:pPr>
            <w:pStyle w:val="Header"/>
            <w:rPr/>
        </w:pPr>
        <w:r>
            <w:rPr/>
            <w:pict>
                <v:shapetype adj="10800" coordsize="21600,21600" id="shapetype_136" o:spt="136" path="m@9,l@10,em@11,21600l@12,21600e">
                    <v:stroke joinstyle="miter"/>
                    <v:formulas>
                        <v:f eqn="val #0"/>
                        <v:f eqn="sum @0 0 10800"/>
                        <v:f eqn="sum @0 0 0"/>
                        <v:f eqn="sum width 0 @0"/>
                        <v:f eqn="prod @2 2 1"/>
                        <v:f eqn="prod @3 2 1"/>
                        <v:f eqn="if @1 @5 @4"/>
                        <v:f eqn="sum 0 @6 0"/>
                        <v:f eqn="sum width 0 @6"/>
                        <v:f eqn="if @1 0 @8"/>
                        <v:f eqn="if @1 @7 width"/>
                        <v:f eqn="if @1 @8 0"/>
                        <v:f eqn="if @1 width @7"/>
                    </v:formulas>
                    <v:handles>
                        <v:h position="@0,21600"/>
                    </v:handles>
                </v:shapetype>
                <v:shape fillcolor="silver" id="PowerPlusWaterMarkObject" o:spid="shape_0" stroked="f" style="position:absolute;margin-left:-0.05pt;margin-top:203.3pt;width:498.5pt;height:257.65pt;rotation:315;mso-position-horizontal:center;mso-position-horizontal-relative:margin;mso-position-vertical:center;mso-position-vertical-relative:margin" type="shapetype_136">
                    <v:path textpathok="t"/>
                    <v:textpath fitshape="t" on="t" string="Tariq" style="font-family:&quot;Liberation Sans&quot;;font-size:1pt" trim="t"/>
                    <w10:wrap type="none"/>
                    <v:fill color2="#3f3f3f" o:detectmouseclick="t" opacity="0.5" type="solid"/>
                    <v:stroke color="#3465a4" endcap="flat" joinstyle="round"/>
                </v:shape>
            </w:pict>
        </w:r>
        <w:bookmarkStart w:id="0" w:name="__UnoMark__0_101710128"/>
        <w:bookmarkStart w:id="1" w:name="__UnoMark__0_101710128"/>
        <w:bookmarkEnd w:id="1"/>
    </w:p>
</w:hdr>

如果您愿意尝试其他包,那么您可以使用Aspose.Words Cloud SDK for Python在 DOC/DOCX/Open Office 文档中添加文本水印。 其免费试用计划为您提供每月 150 次免费 API 调用。

PS:我是 Aspose 的开发人员布道者。

# For complete examples and data files, please go to https://github.com/aspose-words-cloud/aspose-words-cloud-python
# Import module
import os
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-xxxxxxxxx'
client_secret='xxxxxxxxxxxxxxxxxxxxxxxxxxxx'

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

local_test_folder= 'C:/Temp'
localFile = 'Sections.docx'
outputFile= 'Watermark.docx'

requestWatermarkText = asposewordscloud.WatermarkText(text='This is the text', rotation_angle=90)
request = asposewordscloud.models.requests.InsertWatermarkTextOnlineRequest(document=open(os.path.join(local_test_folder, localFile), 'rb'), watermark_text=requestWatermarkText)

result = words_api.insert_watermark_text_online(request)
copyfile(result.document, os.path.join(local_test_folder,outputFile))
print("Result {}".format(result.document))

暂无
暂无

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

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