当前位置: 首页 > news >正文

python在word的页脚插入页码

1、插入简易页码

import win32com.client as win32
from win32com.client import constants
import osdoc_app = win32.gencache.EnsureDispatch('Word.Application')#打开word应用程序
doc_app.Visible = Truedoc = doc_app.Documents.Add()
footer = doc.Sections(1).Footers(constants.wdHeaderFooterPrimary)
footer.Range.Text = "" #清空页脚内容
footer = doc.Sections(1).Footers(constants.wdHeaderFooterPrimary)
footer.LinkToPrevious = False
footer_rng = footer.Range
footer_rng.Text ="自动插入页码 "
footer.PageNumbers.Add(PageNumberAlignment=constants.wdAlignPageNumberRight, FirstPage=True)

如果存在多节的情况,可以修改footer.LinkToPrevious = True,这样页码就是连续的。

添加的页码默认是靠右对齐的。使用wdAlignPageNumberCenter,可以将页码居中。

生成效果:

1619c810af645fa6ca4056c3ddfa8c3c.png?source=d16d100b

 

2、插入自定义页码样式

有的时候,可能需要插入例如第1页,这样的页码样式。就要复杂一些了

需要在字符串“第 页”的中间插入页码。这时候需要用到Field,来实现页码。field可以有很多中作用,通过配置它的type来决定它代表什么。

footer = doc.Sections(1).Footers(constants.wdHeaderFooterPrimary)
footer_rng = footer.Range
footer_rng.Text ="第   页"
rng = footer_rng.Characters(3)
doc.Fields.Add(Range=rng,Type=constants.wdFieldPage)# 在第三个字符的位置插入页码

生成效果:

b6a4adedbb6d7572158c59c991a18b2d.png?source=d16d100b

添加图片注释,不超过 140 字(可选)

 

3、跳过目录页插入页码

当需要跳过目录页、或者封面,只在正文插入页码的时候,需要先在正文前插入分节符——让目录页或者封面与正文分别为不同的节。只对正文所在的节插入页码即可。

import win32com.client as win32
from win32com.client import constants
import osdoc_app = win32.gencache.EnsureDispatch('Word.Application')#打开word应用程序
doc_app.Visible = Truedoc = doc_app.Documents.Add()
parag = doc.Paragraphs.Add()
parag.Range.InsertBreak(constants.wdSectionBreakNextPage)# 在下一页插入分节符parag = doc.Paragraphs.Add()
footer = doc.Sections(2).Footers(constants.wdHeaderFooterPrimary)
footer.Range.Text = "" # 清空页脚内容
footer = doc.Sections(2).Footers(constants.wdHeaderFooterPrimary)
footer.LinkToPrevious = False
footer_rng = footer.Range
footer_rng.Text ="自动插入页码 "
footer.LinkToPrevious = False
footer.PageNumbers.Add(PageNumberAlignment=constants.wdAlignPageNumberRight, FirstPage=True)
footer.PageNumbers.RestartNumberingAtSection = True #新的一节中,页码重新计数
footer.PageNumbers.StartingNumber = 1 # 起始页码为1

 

示例代码效果:

fd9bcf1ebd1ac517100e95a5c4f892ce.png?source=d16d100b

首页没有页码, ]nCVB6

7a66bbb2abe6d6eafe7412ab57afabff.png?source=d16d100b

第二页有插入页码,并且从1开始

 

http://www.lryc.cn/news/475993.html

相关文章:

  • Java面试题十四
  • yarn : 无法加载文件,未对文件 进行数字签名。无法在当前系统上运行该脚本。
  • Hadoop——HDFS
  • 计算机的一些基础知识
  • 学习RocketMQ(记录了个人艰难学习RocketMQ的笔记)
  • 【设计模式】策略模式定义及其实现代码示例
  • list与iterator的之间的区别,如何用斐波那契数列探索yield
  • 抖音店铺数据也就是抖店,如何使用小店数据集来挖掘价值?
  • KubeVirt 安装和配置 Windows虚拟机
  • CM API方式设置YARN队列资源
  • Mysql常用语法一篇文章速成
  • Intel nuc x15 重装系统步骤和注意事项(LAPKC71F、LAPKC71E、LAPKC51E)
  • Linux之实战命令59:iwlist应用实例(九十三)
  • 数据库_SQLite3
  • .Net Framework里演示怎么样使用StringBuilder、Math.Min和String.Format
  • Oracle创建存储过程,创建定时任务
  • <HarmonyOS第一课>应用/元服务上架的课后习题
  • 【Python】探索函数的奥秘:从基础到高级的深度解析(下)
  • ima.copilot:智慧因你而生
  • Vue-$el属性
  • LLC Power Switches and Resonant Tank 笔记
  • Python 如何在 Web 环境中使用 Matplotlib 进行数据可视化
  • C#-数组:一维数组、二维数组、交错数组
  • 动态规划应该如何学习?
  • 【力扣 + 牛客 | SQL题 | 每日4题】牛客SQL热题210,213,212,219
  • Qt 应用开发之 MVC 架构
  • python之字符串总结
  • Flutter鸿蒙next 封装 Dio 网络请求详解:登录身份验证与免登录缓存
  • sql server复制一张表(表结构或表数据)SQL语句整理
  • c语言-进位计数制