python批量替换页眉页脚实例代码

yipeiwu_com6年前Python基础

简介

本文分享的实例代码主要通过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)

总结

以上就是本文关于python批量替换页眉页脚实例代码的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

相关文章

Python正则表达式和re库知识点总结

正则表达式是定义搜索模式的字符序列。通常这种模式被字符串搜索算法用于字符串上的“查找”或“查找和替换”操作,或者用于输入验证。 1. 正则表达式的语法 . 表示任何单个字符 [] 字符集...

python实现计算器功能

python实现计算器功能

本文实例为大家分享了python计算器的具体代码,供大家参考,具体内容如下 主要用到的工具是Python中的Tkinter库 比较简单 直接上图形界面和代码 引用Tkinter库...

python字典多键值及重复键值的使用方法(详解)

python字典多键值及重复键值的使用方法(详解)

在Python中使用字典,格式如下: dict={ key1:value1 , key2;value2 ...} 在实际访问字典值时的使用格式如下: dict[key] 多...

python各种语言间时间的转化实现代码

一 基本知识 millisecond 毫秒 microsecond 微秒 nanosecond 纳秒 1秒=1000毫秒 1毫秒=1000微秒 1微秒=1000纳秒 二 perl pe...

浅谈Python用QQ邮箱发送邮件时授权码的问题

浅谈Python用QQ邮箱发送邮件时授权码的问题

QQ邮箱最新推出了一个授权码,需已验证的手机号向QQ邮箱服务器发送一条短信获得。该授权码用于第三方客户端登录,代替了第三方登录时使用的个人邮箱密码。 在测试过程中遇到两个问题: 1.提示...