这是脚本本身:
def merge_cells(section):
merged_section = [section[0], section[1]] # Сохраняем заголовки
for i in range(2, len(section), 2):
if i + 1 < len(section):
main_row = section[i]
secondary_row = section[i + 1]
merged_row = []
for j in range(len(main_row)):
if j >= 5 and secondary_row[j] == '':
merged_row.append(main_row[j])
else:
merged_row.append(f"{main_row[j]}\n{secondary_row[j]}".strip())
merged_section.append(merged_row)
return merged_section
但是它合并了第 1 列到第 12 列的单元格,而我需要合并 PDF 中第 6 列到第 11 列的单元格。
这是列表本身:
section = [
['DO 33-48(=24V) signal distribution', '', '', '', '', '', '', '', '', '', ''],
['Cable number', 'Wire number', 'Terminal/device number', 'Terminal number', 'Circuit', 'Matching device', 'Parameter type', 'Signal name', 'Tag', 'Logical channel number', 'Module number Channel number', 'Note'],
['', '', '3XT6', '1', '3. 6.1', '24V relay', 'SC', 'Enable', 'B1-10-1_ND_On', 'DO:33', 'A20:TU1', ''],
['', '', '3XT6', '2', '3.6.2', '', '', '', '', '', ''],
['', '', '3XT6', '3', '3. 6.3', '24V relay', 'SC', 'Disable', 'B1-10-1_ND_Off', 'DO:34', 'A20:TU2', ''],
['', '', '3XT6', '4', '3.6.4', '', '', '', '', '', ''],
['', '', '3XT6', '5', '3. 6.5', '24V relay', 'SC', 'Enable', 'B1-31_ND_On', 'DO:35', 'A20:TU3', ''],
['', '', '3XT6', '6', '3.6.6', '', '', '', '', '', ''],
['', '', '3XT6', '7', '3.6. 7', '24V relay', 'SC', 'Disable', 'B1-31_ND_Off', 'DO:36', 'A20:TU4', ''],
['', '', '3XT6', '8', '3.6. 8', '', '', '', '', '', ''],
['', '', '3XT6', '9', '3.6.9', '24V relay', 'SC', 'On', 'B1-13 ND On', 'DO:37', 'A20:TU5', ''],
['', '', '3XT6', '10', '3. 6.10', '', '', '', '', '', ''],
['', '', '3XT6', '11', '3.6.11', '24V Relay', 'SC', 'Off', 'B1-13 ND Off', 'DO:38', 'A20:TU6', ''],
['', '', '3XT6', '12', '3.6. 12', '', '', '', '', '', ''],
['', '', '3XT6', '13', '3.6. 13', '24V relay', 'SC', 'Enable', 'B2-10-2_ND_On', 'DO:39', 'A20:TU7', ''],
['', '', '3XT6', '14', '3.6.14', '', '', '', '', '', '']
]
尝试了不同的选择,但没有任何效果。
而不是做这样的事情
if j >= 5 and secondary_row[j] == '':
# Skip Merging
else:
# Perform the merge
你想像这样改变一切
if j >= 5 and secondary_row[j] == '':
# Perform the merge here instead
else:
# Skip Merging
你的情况刚刚发生了一些逆转。就这样。或者,另一种解决方案是保持现状并将您的条件更改为
if j <= 5 or secondary_row[j] == '':
现在,不再需要两个条件都为真才能跳过合并,而是任一条件都为真即可。对于前 5 列,第一个条件始终为真,我们跳过合并。然后,这取决于
secondary_row[j]
是否为空字符串。