excel - 有没有办法为 Excel 创建 Python 或 VBA 脚本,以按列表编号顺序(大纲中的顺序)合并数据行?

标签 excel vba

我目前正在启动一个项目,我需要管理大型工程和施工契约(Contract)的契约(Contract)要求。
不幸的是,所有确定的项目需求都是通过 PDF 交付的(有数千个 Reqs ......)。从那以后,我将这些 PDF 转换为 Excel 中的电子表格。我最终将使用 .CSV 文件将这些文件导入到我们的 RM 工具中。
我的问题是,所有项目要求的 PDF 都是为了便于阅读而编写的——而不是在电子表格形式中使用。每个部分都像编号列表一样编写,这很好,但我不需要将需求分解到它们的级别。
我需要采用文档的“大纲、列表编号格式”,并能够根据其部分和列表编号将子需求(下面示例中的子项)组合(连接)成行。
现在,我正在手动完成所有这些行的合并,但我不知道如何才能足够快地完成任务。
以下是从客户端查看 PDF 的示例:

第 1-1.1 节:一般
A. 承包商应先“这个”,然后“那个”。

  • “这个”将花费不到这么多钱
  • “那个”将花费更少的钱
    一个。如果“那个”成本更高,则不应添加
    湾。另一个选项是“这个”

  • B. 承包商应将“这个”命名为...
  • 名称应使用正确的语法

  • C. 承包商应在 2022 年之前完成工作

    第 1-2.1 节: Material
    A. 承包商应使用以下三种 Material :


  • 水泥
    一个。水泥只能是灰色的
    __一世。灰色着色必须是这种阴影
    _ii.灰水泥不得失色
    湾。水泥应在现场混合

  • 第 1-2.2 节: Material 供应商
    A. 铝必须由“ABC, Inc”提供
    B. 钢材必须由“DEF, Inc”提供
    C. 水泥必须由“GHI, Inc”提供

    表 1-1:供应商联系信息
    [这里的 table ]

    第 1-3.1 节:景观美化
    ...

    以下是 Excel 文档在其主要状态下的外观。请注意,我们添加了属性来支持每个需求项行,因此这些表中不仅仅是一两列:
    CURRENT EXAMPLE IMAGE
    | [Col A] Requirement List Item | [Col B] Requirement Text                        |
    |-------------------------------|-------------------------------------------------|
    | Section 1-1.1                 | General                                         |
    | A.                            | The contractor shall do “this”, then “that”.    |
    | 1                             | “This” will cost less than this much money      |
    | 2                             | “That” will cost less than this amount of money |
    | a.                            | If “that” costs more, it should not be added    |
    | b.                            | Another option is “this”                        |
    | B.                            | The contractor shall name “this”...             |
    | 1                             | The name should use proper grammar              |
    | C.                            | The contractor shall complete work before 2022  |
    | Section 1-2.1                 | Materials                                       |
    | A.                            | The contractor shall use these three materials: |
    | 1                             | Aluminum                                        |
    | 2                             | Steel                                           |
    | 3                             | Cement                                          |
    | a.                            | Cement should be gray only                      |
    | i.                            | Gray coloring must be this shade                |
    | ii.                           | Gray cement must not lose coloring              |
    | b.                            | Cement should be mixed on-site                  |
    | Section 1-2.2                 | Material Suppliers                              |
    | A.                            | Aluminum must be supplied by “ABC, Inc”         |
    | B.                            | Steel must be supplied by “DEF, Inc”            |
    | C.                            | Cement must be supplied by "GHI, Inc"           |
    | Table 1-1                     | [Table Here]                                    |
    | Section 1-3.1                 | Landscaping                                     |
    | ...                           | ...                                             |
    
    如您所见,我目前的格式只是将每个行项目放在不同的行中,没有明确指示它是父项目还是子项目。很容易判断每个部分中存在哪些项目,但我们需要确定每个顶层包含哪些项目(级别 1 为 A、B、C、D 项目)。

    最后,这是我们希望得到的最终结果。我们只想让数据行按它们的部分编号排序,然后是它们的顶级子需求(在本例中为 A、B、C、D 级):
    FINAL EXAMPLE IMAGE
    有没有人知道如何创建一个 Python 脚本来处理这个任务?我已经尝试了一个半星期来解决这个问题,但我似乎找不到合适的功能来使用。我查找了 Excel 函数/论坛,以及 Concat 和 Merge/TextJoin 的不同用途。
    TLDR:我想获取以编号列表格式(来自 PDF)提供给我们的项目需求数据,并将其组织在 Excel 电子表格中,以仅根据部分和顶级子项数据是什么来分组/连接/合并行(请参阅上面的示例)。
    感谢您提供任何帮助/建议。我真的有兴趣学习脚本编写以使我的工作更轻松,我只是不知道从哪里开始这样的事情。
    这是无法完成任务的 VBA 代码。它似乎在 2-3 段数据后中断:
    Sub GroupLists()
    Dim startRow As Integer
    Dim lastRow As Integer
    Dim outputRow As Integer
    outputRow = 0
    Dim outputStr As String
    Dim col_list As Integer
    Dim col_list_str As String
    Dim col_content As Integer
    Dim col_content_str As String
    
    Dim currentList As String
    Dim i As Integer
    Dim j As Integer
    
    Dim parseSheet As String
    Dim outputSheet As String
    
    Dim startList As Boolean
    startList = False
    
    Dim indent As Integer
    indent = 0
    
    'regex patterns
    Dim regexObject As RegExp
    Set regexObject = New RegExp
    Dim pattern_listHeader As String
    Dim pattern_listUpAlpha As String
    Dim pattern_listLowAlpha As String
    Dim pattern_listNum As String
    pattern_listHeader = "[\d]-"
    pattern_listUpAlpha = "[A-Z]"
    pattern_listLowAlpha = "[a-z]"
    
    'configurable variables
    parseSheet = "Input"
    outputSheet = "Output"
    
    col_list = 2
    col_content = 3
    outputRow = 1
    
    startRow = Worksheets("Control").Cells(2, 1).Value
    lastRow = Worksheets("Control").Cells(2, 2).Value
    
    For i = startRow To lastRow
       
        col_list_str = Worksheets(parseSheet).Cells(i, col_list).Value
        col_content_str = Worksheets(parseSheet).Cells(i, col_content).Value
       
        With regexObject
            .Pattern = "^[\d]-"
        End With
       
        If regexObject.Test(col_list_str) = True Then
            If startList = True Then
                'write output to row, then write current row to output row
                Worksheets(outputSheet).Cells(outputRow, 1).Value = outputStr
                outputRow = outputRow + 1
            End If
           
            outputStr = Worksheets(parseSheet).Cells(i, col_content)
            'write to row
            Worksheets(outputSheet).Cells(outputRow, 1).Value = outputStr
            'increment row
            outputRow = outputRow + 1
            outputStr = ""
            startList = False
        End If
       
        With regexObject
            .Pattern = "^[A-Z]."
        End With
       
        If regexObject.Test(col_list_str) = True Then
            If startList = True Then
                startList = False
            Else
                startList = True
            End If
            outputStr = outputStr & col_list_str & col_content_str & vbNewLine
            'outputRow = outputRow + 1
        End If
       
        If startList = True Then
           
            With regexObject
                .Pattern = "^[a-z]."
            End With
           
            If regexObject.Test(col_list_str) = True Then
                'indent = 1
                outputStr = outputStr & col_list_str & col_content_str & vbNewLine
                'outputRow = outputRow + 1
            End If
           
            With regexObject
                .Pattern = "^[0-9]."
            End With
               
            If regexObject.Test(col_list_str) = True Then
                'indent = indent + 1
                outputStr = outputStr & col_list_str & col_content_str & vbNewLine
                'outputRow = outputRow + 1
            End If
           
           
           
        End If
       
       
       
    Next i
    
    If startList = True Then
        Worksheets(outputSheet).Cells(outputRow, 1).Value = outputStr
    End If
    
    End Sub
    

    enter image description here

    典型的重复如下所示。回应@Dy.Lee 的评论。
    Duplicate numbering items in column B

    最佳答案

    当我写了这么多评论时,我决定发布一个指导答案并删除我的评论,即提供一些指向一种可能的解决方案的指针。

    逻辑:
    我会将所有内容读入一个数组并循环其中的行。应用根据规则处理行的逻辑,例如是从章节开始吗.....
    它是数组第 1 列开头的大写字母吗...是数字吗...
    一个好主意可能是使用辅助函数。例如。

  • 一个辅助函数,它将 Section 之后的大写行向下抓取到下一个大写(之前停止)并返回该文本(作为数组)。
  • 然后将该函数返回值传递给另一个函数,该函数处理基于字母数字的缩进级别(使用适当的 Chr$())等
  • 另一个函数调用将这些值连接成单个字符串 - 这将是与给定键关联的值。
  • 然后用键值对更新字典。

  • 这样,您可以创建键 A 的字典:来自最后一个辅助函数调用的关联文本,并在最后写出到工作表。
    您将需要表等的附加逻辑。表将作为数组添加到字典中,键是第一列的值
    最后,您可以循环并写入工作表。我认为在写出表格时需要谨慎,因为它们可能需要不止一行。在这种情况下,有一个 additional helper function计算工作表(或目标列范围)中任何位置的最后填充行,并通过将 + 1 添加到最后填充的行号来确保将下一个键:值对写到表之后。
    当你写出表格时,你需要测试它的字典键是否包含'table',或者它的值是一个数组;您需要 resize the target cell to the size of the array例如如果 dict(key) = results 那么 targetCell.Resize(UBound(results, 1), UBound(results, 2)) = results想想任何超链接(友好名称与目的地不同的地方)|额外的元数据将被传递。

    提供样本数据:
    我想我会建议你使用 markdown table generator提供当前数据(输入数据)以节省人们写出自己数据的时间。您可以从 Excel 粘贴到 Markdown 生成器中,按生成表,然后复制到该表的剪贴板,使用编辑插入问题。突出显示刚刚粘贴的表格,然后按 Ctrl + K 正确缩进。

    正则表达式:
    从我所见,不需要正则表达式。数据位于源中的单独单元格中。

    标签:
    我认为删除 python 标签,除非你有 python 代码要添加。

    资源:
  • https://bettersolutions.com/vba/strings-characters/builtin-constants.htm

  • 关于缩进的一些有用的常量
    | VBA.Constants | Chr                   | Comments                                                                                                       |
    |---------------|-----------------------|----------------------------------------------------------------------------------------------------------------|
    | vbCr          | Chr(13)               | Carriage return character                                                                                      |
    | vbLf          | Chr(10)               | Linefeed character                                                                                             |
    | vbCrLf        | Chr(13) + Chr(10)     | Carriage return - linefeed combination                                                                         |
    | vbNewLine     | Chr(13) + Chr(10)     | New line character                                                                                             |
    | vbNullChar    | Chr(0)                | Character having a value of 0.                                                                                 |
    | vbNullString  | String having value 0 | Not the same as a zero-length string (""); used for calling external procedures. Cannot be passed to any DLL's |
    | vbTab         | Chr(9)                | Tab character                                                                                                  |
    | vbBack        | Chr(8)                | Backspace character                                                                                            |
    | vbFormFeed    | Chr(12)               | Word VBA Manual - manual page break ?                                                                          |
    | vbVerticalTab | Chr(11)               | Word VBA Manual - manual line break (Shift + Enter)                                                            |
    
  • 字典 -
  • https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/dictionary-object
  • http://www.snb-vba.eu/VBA_Dictionary_en.html

  • 传递和返回数组 - http://www.cpearson.com/excel/passingandreturningarrays.htm
  • 数组和范围 - http://www.cpearson.com/excel/ArraysAndRanges.aspx
  • 类型化函数 - What's the difference between Trim() and Trim$() in VBA?回复:Chr$ v Chr。

  • 替代方法:
    如果使用数组和字典(应该很快)对您不起作用,那么关于如何分离出 block /缩进的逻辑仍然可以在工作表中的实际行上循环应用。但是,您需要根据目标范围中的最后一行来跟踪您写入的位置(您可以使用不同的 last row helper function 并传入用于确定最后填充行的列)。

    关于excel - 有没有办法为 Excel 创建 Python 或 VBA 脚本,以按列表编号顺序(大纲中的顺序)合并数据行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66508739/

    相关文章:

    ms-access - 能否使用VBA设置Access Subform控件属性 "show page header and page footer"

    r - openxlsx 将if公式从R写入excel

    Java - 使用 runtime.getRuntime().exec 运行 Excel

    mysql - 将 excel 中的一列与 excel 中的另一列进行比较并查找重复值

    excel - 在工作簿的所有工作表中查找特定的列标题

    excel - 插入行以用标题分隔数据组

    php - mysql数据导出到excel时出现错误信息

    vba - "Please Wait"弹出消息

    c# - 错误无法评估表达式,因为代码已优化

    excel - 如果下面有另一个表,是否可以一次在表中插入多行?