python批量替换页眉页脚

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import win32com,os,sys,re
from win32com.client import Dispatch, constants

# 打开新的文件
suoyou = os.listdir('d:\\daizhuan')
#print suoyou
for i in suoyou:
    wenjian_name = os.path.join('d:\\daizhuan',i)
    #print wenjian_name
    if os.path.isfile(wenjian_name):  
        w = win32com.client.Dispatch('Word.Application') 
        w.Visible = 0
        w.DisplayAlerts = 0
        daizhuan = 'd:\\daizhuan\\%s' % i #准备替换的文件夹
        wancheng = 'd:\\wancheng\\%s' % i #替换完成后输出的目录
        doc = w.Documents.Open('d:\\biaozhun\\biaozhun.doc')
        w.ActiveDocument.Sections[0].Headers[0].Range.Copy()
        wc = win32com.client.constants
        doc.Close()

        doc2= w.Documents.Open( daizhuan)
        w.ActiveDocument.Sections[0].Headers[0].Range.Paste()
        w.ActiveDocument.SaveAs(wancheng)
        doc2.Close()

        doc3 = w.Documents.Open( 'd:\\biaozhun\\biaozhun.doc')
        w.ActiveDocument.Sections[0].Footers[0].Range.Copy()
        doc3.Close()

        doc4= w.Documents.Open( daizhuan)
        w.ActiveDocument.Sections[0].Footers[0].Range.Paste()
        doc4.Close()
        try:
            w.Documents.Close()
            w.Quit()
        except Exception , e:
            print str(e)

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值