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

相关文章

3个用于数据科学的顶级Python库

3个用于数据科学的顶级Python库

Python有许多吸引力,如效率,代码可读性和速度,使其成为数据科学爱好者的首选编程语言。Python通常是希望升级其应用程序功能的数据科学家和机器学习专家的首选。 由于其广泛的用途,P...

值得收藏,Python 开发中的高级技巧

Python 开发中有哪些高级技巧?这是知乎上一个问题,我总结了一些常见的技巧在这里,可能谈不上多高级,但掌握这些至少可以让你的代码看起来 Pythonic 一点。如果你还在按照类C语言...

python自动化实现登录获取图片验证码功能

python自动化实现登录获取图片验证码功能

主要记录一下:图片验证码 1.获取登录界面的图片 2.获取验证码位置 3.在登录页面截取验证码保存 4.调用百度api识别(目前准确率较高的识别图片api) 本次登录的系统页面,可以看到...

Pandas:Series和DataFrame删除指定轴上数据的方法

如下所示: import numpy as np import pandas as pd from pandas import Series,DataFrame 一、drop方法:...

Python中List.count()方法的使用教程

 count()方法返回obj出现在列表的次数。 语法 以下是count()方法的语法: list.count(obj) 参数   &nbs...