python docx合并单元格,使用python-docx跨表格中的多个列的单元格

I'm trying to create a table that looks like this, using the python-docx module.

67a7e27e31320f7ae3f7b940680d25cd.png

Working from the example code for creating a table in example-makedocument.py and reading through the code in docx.py, I thought something similar to this would work:

tbl_rows = [ ['A1'],

['B1', 'B2' ],

['C1', 'C2' ] ]

tbl_colw = [ [100],

[25, 75],

[25, 75] ]

tbl_cwunit = 'pct'

body.append(table(tbl_rows, colw=tbl_colw, cwunit=tbl_cwunit))

however this corrupts the docx document, and when Word recovers the document the table is shown as this:

dfc0986678712634f6f268ef7f71cbbc.png

How can I get a row to properly span multiple columns using python-docx?

解决方案

I added a merge_cells() function to the row class of python-docx, that can merge any cells on a single row. I'm hoping it will get included in python-docx. In the meantime, you can grab it from my github "merge_cells_feature" python-docx branch.

I'm copying the example I wrote on the pull request here for reference:

from docx import Document

doc = Document()

table = doc.add_table(6,6)

header_row = table.rows[0]

header_row.merge_cells()

# args can also be passed to merge_cells(mergeStart, mergeStop) in order to

# implement any kind of merge on the same row, such as:

table.rows[1].merge_cells(0,3) # This will merge the cells indexed from 0 to 2.

python-docx,要合并单元格可以使用merge()方法。该方法接受一个参数,即要合并的目标单元格。合并后会返回一个新的合并后的单元格对象。下面是一个示例代码: ```python from docx import Document doc = Document() table = doc.add_table(5, 3, style="Table Grid") # 获取要合并的单元格 cell1 = table.cell(0, 0) cell2 = table.cell(0, 1) # 合并单元格 merged_cell = cell1.merge(cell2) # 修改合并后的单元格文本 merged_cell.text = "合并后的单元格" # 保存文档 doc.save("merged_table.docx") ``` 在这个示例,我们首先创建了一个包含5行3表格。然后,我们获取了要合并的两个单元格cell1和cell2。接下来,我们使用merge()方法将这两个单元格合并成一个新的单元格merged_cell。最后,我们修改了合并后的单元格的文本内容为"合并后的单元格"。最后,我们保存了文档。 请注意,合并单元格的操作会改变表格的结构,因此在合并单元格之前,请确保你已经理解了表格的结构和要合并的单元格的位置。 #### 引用[.reference_title] - *1* [【python-docx】添加表格合并单元格、设置样式](https://blog.csdn.net/qq_39147299/article/details/125414499)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [python docx处理word文档表格合并问题](https://blog.csdn.net/u013546508/article/details/88687661)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [python-docx 合并单元格](https://blog.csdn.net/qq1261275789/article/details/126380020)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值