Python修改docx文档格式

任务:原docx文档修改标题和页面宽高。

步骤:1、原docx文档解压为document.xml(见最后)

2、通过xml解释替换标签属性值,保存为document_format.xml

3、还原document_format.xml为原docx文档。

# -*- coding: UTF-8 -*-
#!/usr/bin/python

from bs4 import BeautifulSoup
soup = BeautifulSoup(open("document.xml", encoding='utf-8'),'xml')
 
# print("temp(tag):",soup.temp)
# print("temp(tag)name:",soup.temp.name)
# print("temp(tag)attribute:",soup.temp.attrs)
# #可以通过键值的形式查找
# print("temp(tag)attribute:",soup.temp['information'])

#更改标题
for tag in soup.find_all("w:t"):#标签是区分大小写的
	if tag.string=="2020年全市重点信息系统责任单位":
		print(tag.string)#打印标签包裹的文本内容
		tag.string.replace_with("2021年全市重点信息系统责任单位")#替换标签包裹的文本内容
#更改页宽页高
for tag in soup.find_all("w:pgSz"):#标签是区分大小写的
	print(tag.parent.name)
	if tag.parent.name == "sectPr" and 'w:w' in tag.attrs and 'w:h' in tag.attrs:
		tag.attrs.update({"w:h":"16000"})#更改属性值:页高
		tag.attrs.update({"w:w":"12000"})#更改属性值:页宽

#修改结果写入文档
with open('document_format.xml', 'w', encoding='utf-8') as fp:
    fp.write(soup.prettify()) 

附原docx文档对应的document.xml

<?xml version="1.0" encoding="UTF-8" standalone="true"?>
<w:document mc:Ignorable="w14 w15 wp14" xmlns:wpsCustomData="http://www.wps.cn/officeDocument/2013/wpsCustomData" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas">
<w:body>
<w:p>
<w:pPr>
<w:spacing w:lineRule="exact" w:line="560"/>
<w:rPr>
<w:rFonts w:eastAsia="黑体"/>
<w:sz w:val="32"/>
<w:szCs w:val="32"/>
</w:rPr>
</w:pPr>
<w:r>
<w:rPr>
<w:rFonts w:eastAsia="黑体" w:hAnsi="黑体" w:ascii="黑体" w:hint="eastAsia"/>
<w:sz w:val="32"/>
<w:szCs w:val="32"/>
</w:rPr>
<w:t>附件</w:t>
</w:r>
</w:p>
<w:p>
<w:pPr>
<w:pStyle w:val="8"/>
<w:spacing w:lineRule="exact" w:line="560"/>
<w:ind w:firstLine="880"/>
<w:rPr>
<w:rFonts w:eastAsia="方正小标宋简体" w:hAnsi="方正小标宋简体" w:ascii="方正小标宋简体" w:hint="eastAsia"/>
<w:sz w:val="44"/>
<w:szCs w:val="44"/>
</w:rPr>
</w:pPr>
</w:p>
<w:p>
<w:pPr>
<w:pStyle w:val="8"/>
<w:spacing w:lineRule="exact" w:line="560"/>
<w:ind w:firstLine="880"/>
<w:rPr>
<w:rFonts w:eastAsia="方正小标宋简体" w:hAnsi="方正小标宋简体" w:ascii="方正小标宋简体" w:hint="eastAsia"/>
<w:sz w:val="44"/>
<w:szCs w:val="44"/>
</w:rPr>
</w:pPr>
<w:r>
<w:rPr>
<w:rFonts w:eastAsia="方正小标宋简体" w:hAnsi="方正小标宋简体" w:ascii="方正小标宋简体" w:hint="eastAsia"/>
<w:sz w:val="44"/>
<w:szCs w:val="44"/>
</w:rPr>
<w:t>2020年全市重点信息系统责任单位</w:t>
</w:r>
</w:p>
<w:p>
<w:pPr>
<w:pStyle w:val="8"/>
<w:spacing w:lineRule="exact" w:line="560" w:afterLines="100"/>
<w:ind w:firstLine="0" w:firstLineChars="0"/>
<w:jc w:val="center"/>
<w:rPr>
<w:rFonts w:eastAsia="方正小标宋简体" w:hAnsi="方正小标宋简体" w:ascii="方正小标宋简体" w:hint="eastAsia"/>
<w:sz w:val="44"/>
<w:szCs w:val="44"/>
</w:rPr>
</w:pPr>
<w:r>
<w:rPr>
<w:rFonts w:eastAsia="方正小标宋简体" w:hAnsi="方正小标宋简体" w:ascii="方正小标宋简体" w:hint="eastAsia"/>
<w:sz w:val="44"/>
<w:szCs w:val="44"/>
</w:rPr>
<w:t>络安全自查工作说明</w:t>
</w:r>
</w:p>
<w:p>
<w:pPr>
<w:pStyle w:val="8"/>
<w:spacing w:lineRule="exact" w:line="560" w:afterLines="100"/>
<w:ind w:firstLine="0" w:firstLineChars="0"/>
<w:jc w:val="center"/>
<w:rPr>
<w:rFonts w:eastAsia="方正小标宋简体" w:hAnsi="方正小标宋简体" w:ascii="方正小标宋简体" w:hint="eastAsia"/>
<w:sz w:val="44"/>
<w:szCs w:val="44"/>
</w:rPr>
</w:pPr>
</w:p>
<w:p>
<w:pPr>
<w:pStyle w:val="8"/>
<w:spacing w:lineRule="exact" w:line="560"/>
<w:ind w:firstLine="640"/>
<w:rPr>
<w:rFonts w:eastAsia="仿宋_GB2312" w:hAnsi="Times New Roman" w:ascii="Times New Roman" w:hint="eastAsia"/>
<w:sz w:val="32"/>
<w:szCs w:val="32"/>
</w:rPr>
</w:pPr>
<w:r>
<w:rPr>
<w:rFonts w:eastAsia="仿宋_GB2312" w:hAnsi="Times New Roman" w:ascii="Times New Roman" w:hint="eastAsia"/>
<w:sz w:val="32"/>
<w:szCs w:val="32"/>
</w:rPr>
<w:t>通过访问以下地址(各百度网盘地址获取内容相同)获取自查采集工具安装包及用户手册:</w:t>
</w:r>
</w:p>
<w:p>
<w:pPr>
<w:spacing w:lineRule="exact" w:line="560"/>
<w:ind w:leftChars="300" w:left="660"/>
<w:rPr>
<w:rFonts w:eastAsia="仿宋_GB2312"/>
<w:sz w:val="32"/>
<w:szCs w:val="32"/>
</w:rPr>
</w:pPr>
<w:r>
<w:rPr>
<w:rFonts w:eastAsia="仿宋_GB2312"/>
<w:sz w:val="32"/>
<w:szCs w:val="32"/>
</w:rPr>
<w:t>1</w:t>
</w:r>
<w:r>
<w:rPr>
<w:rFonts w:eastAsia="仿宋_GB2312" w:hint="eastAsia"/>
<w:sz w:val="32"/>
<w:szCs w:val="32"/>
</w:rPr>
<w:t>、</w:t>
</w:r>
<w:r>
<w:rPr>
<w:rFonts w:eastAsia="仿宋_GB2312"/>
<w:sz w:val="32"/>
<w:szCs w:val="32"/>
</w:rPr>
<w:t xml:space="preserve">https://pan.baidu.com/s/1zWbz5UCUTpqmCCahQ3jw </w:t>
</w:r>
</w:p>
<w:p>
<w:pPr>
<w:spacing w:lineRule="exact" w:line="560"/>
<w:ind w:leftChars="300" w:left="660"/>
<w:rPr>
<w:rFonts w:eastAsia="仿宋_GB2312"/>
<w:sz w:val="32"/>
<w:szCs w:val="32"/>
</w:rPr>
</w:pPr>
<w:r>
<w:rPr>
<w:rFonts w:eastAsia="仿宋_GB2312" w:hint="eastAsia"/>
<w:sz w:val="32"/>
<w:szCs w:val="32"/>
</w:rPr>
<w:t>提取码:</w:t>
</w:r>
<w:r>
<w:rPr>
<w:rFonts w:eastAsia="仿宋_GB2312"/>
<w:sz w:val="32"/>
<w:szCs w:val="32"/>
</w:rPr>
<w:t>2</w:t>
</w:r>
<w:r>
<w:rPr>
<w:rFonts w:eastAsia="仿宋_GB2312" w:hint="default"/>
<w:sz w:val="32"/>
<w:szCs w:val="32"/>
<w:lang w:val="en-US"/>
</w:rPr>
<w:t>1</w:t>
</w:r>
<w:r>
<w:rPr>
<w:rFonts w:eastAsia="仿宋_GB2312"/>
<w:sz w:val="32"/>
<w:szCs w:val="32"/>
</w:rPr>
<w:t>py</w:t>
</w:r>
</w:p>
<w:p>
<w:pPr>
<w:spacing w:lineRule="exact" w:line="560"/>
<w:ind w:leftChars="300" w:left="660"/>
<w:rPr>
<w:rFonts w:eastAsia="仿宋_GB2312"/>
<w:sz w:val="32"/>
<w:szCs w:val="32"/>
</w:rPr>
</w:pPr>
<w:r>
<w:rPr>
<w:rFonts w:eastAsia="仿宋_GB2312"/>
<w:sz w:val="32"/>
<w:szCs w:val="32"/>
</w:rPr>
<w:t>2</w:t>
</w:r>
<w:r>
<w:rPr>
<w:rFonts w:eastAsia="仿宋_GB2312" w:hint="eastAsia"/>
<w:sz w:val="32"/>
<w:szCs w:val="32"/>
</w:rPr>
<w:t>、</w:t>
</w:r>
<w:r>
<w:rPr>
<w:rFonts w:eastAsia="仿宋_GB2312"/>
<w:sz w:val="32"/>
<w:szCs w:val="32"/>
</w:rPr>
<w:t>https://pan.baidu.com/s/1jsEznmNDST</w:t>
</w:r>
<w:r>
<w:rPr>
<w:rFonts w:eastAsia="仿宋_GB2312" w:hint="default"/>
<w:sz w:val="32"/>
<w:szCs w:val="32"/>
<w:lang w:val="en-US"/>
</w:rPr>
<w:t>s2</w:t>
</w:r>
<w:r>
<w:rPr>
<w:rFonts w:eastAsia="仿宋_GB2312"/>
<w:sz w:val="32"/>
<w:szCs w:val="32"/>
</w:rPr>
<w:t xml:space="preserve">UosKgPw6Gg </w:t>
</w:r>
</w:p>
<w:p>
<w:pPr>
<w:spacing w:lineRule="exact" w:line="560"/>
<w:ind w:leftChars="300" w:left="660"/>
<w:rPr>
<w:rFonts w:eastAsia="仿宋_GB2312" w:hint="default"/>
<w:sz w:val="32"/>
<w:szCs w:val="32"/>
<w:lang w:val="en-US"/>
</w:rPr>
</w:pPr>
<w:r>
<w:rPr>
<w:rFonts w:eastAsia="仿宋_GB2312" w:hint="eastAsia"/>
<w:sz w:val="32"/>
<w:szCs w:val="32"/>
</w:rPr>
<w:t>提取码</w:t>
</w:r>
<w:r>
<w:rPr>
<w:rFonts w:eastAsia="仿宋_GB2312"/>
<w:sz w:val="32"/>
<w:szCs w:val="32"/>
</w:rPr>
<w:t>: 51m</w:t>
</w:r>
<w:r>
<w:rPr>
<w:rFonts w:eastAsia="仿宋_GB2312" w:hint="default"/>
<w:sz w:val="32"/>
<w:szCs w:val="32"/>
<w:lang w:val="en-US"/>
</w:rPr>
<w:t>w</w:t>
</w:r>
</w:p>
<w:p>
<w:pPr>
<w:spacing w:lineRule="exact" w:line="560"/>
<w:ind w:leftChars="300" w:left="660"/>
<w:rPr>
<w:rFonts w:eastAsia="仿宋_GB2312"/>
<w:sz w:val="32"/>
<w:szCs w:val="32"/>
</w:rPr>
</w:pPr>
<w:r>
<w:rPr>
<w:rFonts w:eastAsia="仿宋_GB2312"/>
<w:sz w:val="32"/>
<w:szCs w:val="32"/>
</w:rPr>
<w:t>3</w:t>
</w:r>
<w:r>
<w:rPr>
<w:rFonts w:eastAsia="仿宋_GB2312" w:hint="eastAsia"/>
<w:sz w:val="32"/>
<w:szCs w:val="32"/>
</w:rPr>
<w:t>、</w:t>
</w:r>
<w:r>
<w:rPr>
<w:rFonts w:eastAsia="仿宋_GB2312"/>
<w:sz w:val="32"/>
<w:szCs w:val="32"/>
</w:rPr>
<w:t xml:space="preserve">https://pan.baidu.com/s/1DlMV2C-DIDNcqG7bD3fg </w:t>
</w:r>
</w:p>
<w:p>
<w:pPr>
<w:spacing w:lineRule="exact" w:line="560"/>
<w:ind w:leftChars="300" w:left="660"/>
<w:rPr>
<w:rFonts w:eastAsia="仿宋_GB2312"/>
<w:sz w:val="32"/>
<w:szCs w:val="32"/>
</w:rPr>
</w:pPr>
<w:r>
<w:rPr>
<w:rFonts w:eastAsia="仿宋_GB2312" w:hint="eastAsia"/>
<w:sz w:val="32"/>
<w:szCs w:val="32"/>
</w:rPr>
<w:t>提取码:</w:t>
</w:r>
<w:r>
<w:rPr>
<w:rFonts w:eastAsia="仿宋_GB2312"/>
<w:sz w:val="32"/>
<w:szCs w:val="32"/>
</w:rPr>
<w:t>zgut</w:t>
</w:r>
<w:bookmarkStart w:name="_GoBack" w:id="0"/>
<w:bookmarkEnd w:id="0"/>
</w:p>
<w:p>
<w:pPr>
<w:pStyle w:val="8"/>
<w:spacing w:lineRule="exact" w:line="560"/>
<w:ind w:firstLine="640"/>
<w:rPr>
<w:rFonts w:eastAsia="仿宋_GB2312" w:hAnsi="Times New Roman" w:ascii="Times New Roman" w:hint="eastAsia"/>
<w:sz w:val="32"/>
<w:szCs w:val="32"/>
</w:rPr>
</w:pPr>
</w:p>
<w:p>
<w:pPr>
<w:spacing w:lineRule="exact" w:line="560"/>
<w:rPr>
<w:rFonts w:eastAsia="仿宋_GB2312" w:hAnsi="仿宋_GB2312" w:ascii="仿宋_GB2312"/>
<w:color w:val="000000"/>
<w:sz w:val="32"/>
</w:rPr>
</w:pPr>
</w:p>
<w:p>
<w:pPr>
<w:spacing w:lineRule="atLeast" w:line="220"/>
</w:pPr>
</w:p>
<w:sectPr>
<w:pgSz w:w="11906" w:h="16838"/>
<w:pgMar w:left="1800" w:gutter="0" w:footer="708" w:header="708" w:bottom="1440" w:right="1800" w:top="1440"/>
<w:cols w:space="708" w:num="1"/>
<w:docGrid w:charSpace="0" w:linePitch="360"/>
</w:sectPr>
</w:body>
</w:document>

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值