Skip to main content
 首页 » 编程设计

python之使用 Python 向 Word 文档添加页码

2024年11月01日10bluestorm

有没有办法使用Python win32com在Word文档的右下角添加页码?我能够添加页眉和页脚,但我找不到以总页数的页码格式添加页码的方法(例如:第 1 页,共 5 页)

下面是向页面添加居中页眉和页脚的代码

from win32com.client import Dispatch as MakeDoc 
filename = name + '.doc' 
WordDoc = MakeDoc("Word.Application") 
WordDoc = WordDoc.Documents.Add() 
WordDoc.Sections(1).Headers(1).Range.Text = name 
WordDoc.Sections(1).Headers(1).Range.ParagraphFormat.Alignment = 1 
WordDoc.Sections(1).Footers(1).Range.Text = filename 
WordDoc.Sections(1).Footers(1).Range.ParagraphFormat.Alignment = 1 

谢谢

请您参考如下方法:

要插入页码,请使用以下语句:

WordDoc.Sections(1).Footers(1).PageNumbers.Add(2,True) 
WordDoc.Sections(1).Footers(1).PageNumbers.NumberStyle = 57 

但是,页码的格式是-页码-。用于插入页码的文档是 here , 数字样式为 here