python - 尝试将单元格与 python-docx 合并
问题描述
我正在尝试为 docx 文档合并一堆单元格,如下所示:
from docx import Document
from docx.shared import Inches
document = Document()
document.add_heading('Document Title', 0)
records = (
(3, '101', 'Spam'),
(7, '422', 'Eggs'),
(4, '631', 'Spam, spam, eggs, and spam')
)
table = document.add_table(rows=3, cols=3)
hdr_cells = table.rows[0].cells
hdr_cells[0].text = 'Qty'
hdr_cells[1].text = 'Id'
hdr_cells[2].text = 'Desc'
for qty, id, desc in records:
row_cells = table.add_row().cells
row_cells[0].text = str(qty)
row_cells[1].text = id
row_cells[2].text = desc
a = table.cell(0, 0)
b = table.cell(1, 1)
c = table.cell(2, 1)
d = table.cell(1, 2)
A = a.merge(b)
B = A.merge(c)
C = B.merge(d)
document.save('demo.docx')
但我得到:
Traceback (most recent call last):
File "pdocx_error.py", line 32, in <module>
C = B.merge(d)
File "/home/martin/myvenv/lib/python3.8/site-packages/docx/table.py", line 232, in merge
merged_tc = tc.merge(tc_2)
File "/home/martin/myvenv/lib/python3.8/site-packages/docx/oxml/table.py", line 443, in merge
top, left, height, width = self._span_dimensions(other_tc)
File "/home/martin/myvenv/lib/python3.8/site-packages/docx/oxml/table.py", line 639, in _span_dimensions
raise_on_tee_shaped(self, other_tc)
File "/home/martin/myvenv/lib/python3.8/site-packages/docx/oxml/table.py", line 632, in raise_on_tee_shaped
raise InvalidSpanError('requested span not rectangular')
docx.exceptions.InvalidSpanError: requested span not rectangular
这里出了什么问题?
解决方案
推荐阅读
- sql - 错误 156 严重性 15 状态 1 使用语法 CASE WHEN ELSE END
- java - 基于不同和第二个谓词的过滤器列表
- flutter - 在 Flutter 中单击具有多个片段的 Navigation Drawer 中的每个项目会重新加载屏幕
- java - 在opengl中的矩形窗口上绘制一个正方形
- c# - 处理 SQLite EF Core 限制 - 基础操作
- vb.net - 我想从串口接收数据,当我收到它时,每个数据都从新行开始,我希望旧数据被新数据更改
- arrays - 如何从love2d中的数组中获取随机索引之一
- r - 在r中提取具有特定列值的数据
- javascript - 为什么我的 React 应用程序在 Chrome Incognito 上启动时会崩溃(由于 Redux)?
- linux - QEMU 在哪里映射来宾操作系统的内核页面?