首页 > 解决方案 > 使用python docxcompose合并文档,不同的header如何处理?

问题描述

我有很多文档,这些文档只有一页,但标题不同,我使用pythondocxcompose合并这些文档,但是这个操作会将所有标题更改为第一个文档或主文档,有什么办法避免这个?我只是希望这些文件有不同的标题。

我在is_linked_to_previous=False合并这些文档时添加,但它不起作用

这是我使用的代码:

def megerDocToOne(self, seperatepath, megerpathAndFileName):

        original_docx_path = seperatepath
        new_docx_path= megerpathAndFileName

        all_word=os.listdir(original_docx_path)
        all_file_path=[]
        for file_name in all_word:
            all_file_path.append(original_docx_path+file_name)
        master=Document(all_file_path[0])

        middle_new_docx=Composer(master)
        num=0
        for word in all_file_path:
            word_document=Document(word)
            word_document.add_section(WD_SECTION_START.NEW_PAGE)
            header = word_document.sections[0].header
            header.is_linked_to_previous = False

            if num!=0:
                middle_new_docx.append(word_document,True)
            num=num+1
        middle_new_docx.save(new_docx_path)

标签: pythonms-wordpython-docx

解决方案


推荐阅读