当前位置: 移动技术网 > IT编程>脚本编程>Python > Python win32com模块 合并文件夹内多个docx文件为一个docx

Python win32com模块 合并文件夹内多个docx文件为一个docx

2019年11月08日  | 移动技术网IT编程  | 我要评论

英雄传奇私服,吴秀波 红孩儿,石家庄个人房屋出租

python win32com模块 合并文件夹内多个docx文件为一个docx

#!/usr/bin/env python     
# -*- coding: utf-8 -*-     
from win32com.client import dispatch
import os,sys
#import pandas as pd

#下面三句是为了能取得系统自带常量
from win32com.client import constants as con
#from win32com.client.gencache import ensuredispatch
#ensuredispatch('word.application')
def mkdir(path):
    # 引入模块
    # import os

    # 去除首位空格
    # path=path.strip()
    # 去除尾部 \ 符号
    # path=path.rstrip("\\")

    # 判断路径是否存在
    # 存在     true
    # 不存在   false
    isexists=os.path.exists(path)

    # 判断结果
    if not isexists:
        # 如果不存在则创建目录
        # 创建目录操作函数
        os.makedirs(path) 
        print(path+'创建成功')
        return true
    else:
        # 如果目录存在则不创建,并提示目录已存在
        print(path+' 目录已存在')
        return false


pwd=__file__
pwd=os.path.dirname(pwd)
print(pwd)

mkdir(os.path.join(pwd,'hebing'))
#xlapp=dispatch('excel.application')
wdapp=dispatch('word.application')
#xlapp.visible=1

docb=wdapp.documents.add()
#=====页面设置======
ps=56.7
docb.pagesetup.topmargin=ps
docb.pagesetup.bottommargin=ps
docb.pagesetup.leftmargin=ps
docb.pagesetup.rightmargin=ps
docb.saveas(pwd + '\\hebing\\hebing.docx',14)
#======保存文档=========


for root,dirs,files in os.walk(os.path.join(pwd,'放word文档的文件夹'),topdown=false):
    print(files)
    for name in files:
        doct=wdapp.documents.open(os.path.join(root,name))
        #doct.range(0,0)#光标定位文档开始
        wdapp.selection.wholestory()#全选word文档
        wdapp.selection.copy()
        docb.activate()
        #wdapp.selection.endkey(unit=con.wdline)       
        docb.range()#光标定位文档结束
        wdapp.selection.delete()
        wdapp.selection.paste()
        
        doct.close()
    
docb.save()
docb.close()
wdapp.quit()

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网