我正在使用似乎不支持水印的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:"Liberation Sans";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>
答案 0 :(得分:0)
如果您愿意尝试其他一些软件包,那么您可以使用 Aspose.Words Cloud SDK for Python 在 DOC/DOCX/Open Office 文档中添加文本水印。其免费试用计划为您提供每月 150 次免费 API 调用。
P.S:我是 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))