遍历目录下所有需要文档合并

标题遍历目录下所有需要文档合并

1. 需要第三方模块 pywin32

pip install pywin32

2. 示例

import os
import re

allpath=[]
def getallfile(path):
    allfilelist=os.listdir(path)
    # 遍历该文件夹下的所有目录或者文件
    for file in allfilelist:
        filepath=os.path.join(path,file)
        # 如果是文件夹,递归调用函数
        if os.path.isdir(filepath):
            getallfile(filepath)
        # 如果不是文件夹,保存文件路径及文件名
        elif os.path.isfile(filepath):
            allpath.append(filepath)
    return allpath

if __name__ == "__main__":

    import win32com.client as win32

    rootdir ="H:笔记"
    files = getallfile(rootdir)
    filist=[]

    for fi in files:
        result=re.search(r'.*\.docx$',fi)
        if result != None:
            file_data=result.group()
            print(">>>>",file_data)
            filist.insert(0,file_data)
    print(filist)
    word = win32.gencache.EnsureDispatch('Word.Application')
    # 非可视化运行
    word.Visible = False
    output = word.Documents.Add()  # 新建合并后空白文档
    # 需要合并的文档路径,这里有个文档1.docx,2.docx,3.docx.
    for file in filist:
        output.Application.Selection.Range.InsertFile(file)  # 拼接文档
    # 获取合并后文档的内容
    doc = output.Range(output.Content.Start, output.Content.End)
    # doc.Font.Name = "黑体"  # 设置字体
    output.SaveAs(r'H:\acd.docx')  # 保存
    output.Close()

一万小时定律
人们眼中的天才之所以卓越非凡,并非天资超人一等,而是付出了持续不断的努力。
一万小时的锤炼是任何人从平凡变成超凡的必要条件。

批量合并DOC文档,一键搞定.atch DOC AND DOCX Converter is an easy to use software solution designed to convert Word 2003 documents into Word 2007 files and vice-versa. Designed to handle DOC and DOCX documents exclusively, the application sports a clean and user-friendly interface to help it serve this purpose. While the main window comes with nifty buttons at the top to save or start a new batch conversion, the rest of the interface is mostly based on the list of files to be processed, along with conversion results. Those uninitiated should have no problem in figuring out how to use the application and in case they need assistance when it comes to the conversion process, there's also a help file to lend them a hand. The help manual comprises tons of information on all features, so it's recommended to have a look in there as well. Although everything's easy as pie, not the same thing can be said about the installation process. There are two different deployment modes at your disposal, quick and custom, and even if the first sounds like the best idea, we advise you to choose the latter. The installer also includes some unnecessary software and even attempts to change the default search and homepage in your browser; choosing the custom installation mode is the only way to skip installing them too. Overall, Batch DOC AND DOCX Converter may come in handy to many users, not only because it works so smooth but also thanks to the fact that it also adds a special entry in the Windows Context Menu to convert files on the go.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值