【问题标题】:How to merge multiple word file into one word file according to the content如何根据内容将多个word文件合并为一个word文件
【发布时间】:2022-10-24 14:43:11
【问题描述】:

比如有3个word文件(名称:word_1、word_2、word_3),我想把所有内容合并到word_1中。

合并前:

Content in word_1:
1.1.1 Hello World
      testing text 111

1.1.2 Hello America
      testing text 222

1.1.3 Hello USA


1.1.4 Hello Korea


1.1.5 Hello Japan

Content in word_2:
1.1.3 Hello USA
      testing text 333

1.1.4 Hello Korea
      testing text 444

Content in word_3:
1.1.5 Hello Japan
      testing text 888

1.1.6 Hello Germany
      testing text 999

合并后:

1.1.1 Hello World
      testing text 111

1.1.2 Hello America
      testing text 222

1.1.3 Hello USA
      testing text 333

1.1.4 Hello Korea
      testing text 444

1.1.5 Hello Japan
      testing text 888

基本上它是由主题旁边的 ID 合并(即 1.X.X,X.X.X),我假设没有重复的 ID,每个 ID 都是唯一的。目前我找到了一些资源作为下面的代码,我只能将word文件合并到另一个word文件。非常感谢你的帮助。如果能用python实现也可以。

Option Explicit

Sub MergeFilesInAFolderlntoOneDoc()
Dim dlgFile As FileDialog
Dim wdApp As Object, oRng As Object
Dim objDoc As Object, objNewDoc As Object
Dim StrFolder As String, strFile As String
    Set dlgFile = Application.FileDialog(msoFileDialogFolderPicker)

    With dlgFile
        If .Show = -1 Then
            StrFolder = dlgFile.SelectedItems.Item(1) & Chr(92)
        Else
            MsgBox ("No folder is selected!")
            Exit Sub
        End If
    End With

    On Error Resume Next
    Set wdApp = GetObject(, "Word.Application")
    If Err Then
        Set wdApp = CreateObject("Word.Application")
    End If
    On Error GoTo 0

    strFile = Dir(StrFolder & "*.docx", vbNormal)
    With wdApp
        .Visible = True
        Set objNewDoc = .Documents.Add
        While strFile <> ""
            Set objDoc = .Documents.Open(FileName:=StrFolder & strFile)
            With objNewDoc
                Set oRng = .Range
                If Len(oRng) > 1 Then
                    oRng.Collapse 0
                    oRng.InsertBreak 7
                End If
                oRng.Collapse 0
                oRng.FormattedText = objDoc.Range.FormattedText
                objDoc.Close 0
            End With
            DoEvents
            strFile = Dir()
        Wend
        objNewDoc.Activate
    End With
End Sub

【问题讨论】:

    标签: python vba ms-word


    【解决方案1】:

    使用regex 和一些字符串操作

    a = """Content in word_1:
    1.1.1 Hello World
          testing text 111
    
    1.1.2 Hello America
          testing text 222
    
    1.1.3 Hello USA
    
    
    1.1.4 Hello Korea
    
    
    1.1.5 Hello Japan"""
    
    b = """Content in word_2:
    1.1.3 Hello USA
          testing text 333
    
    1.1.4 Hello Korea
          testing text 444"""
    
    c = """Content in word_3:
    1.1.5 Hello Japan
          testing text 888
    
    1.1.6 Hello Germany
          testing text 999"""
    
    import re
    def create_dict(text):
        d = {}
        res = re.finditer(r"[1-9]{1}.[1-9]{1}.[1-9]{1}", text)
        l = list(res)
        for x, y  in zip(l, l[1:]):
            d[x.group()] = text[x.span()[1]: y.span()[0]]
        d[l[-1].group()] = text[l[-1].span()[1]: ]
        return d
    
    
    def merge_text(parent, *texts):
        temp_d = {parent.splitlines()[0]: create_dict(parent)}
        for d in texts:
            temp_d[parent.splitlines()[0]].update(create_dict(d))
        return "
    ".join([parent.splitlines()[0], *temp_d[parent.splitlines()[0]].values()])
    merge_text(a, b, c)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多