python创建word表格_如何使用python docx将表格边框添加到word doc

我正在尝试使用Python的docx模块创建word文档.

但是我无法为其添加表格边框.

我的代码如下:

import docx

from docx import Document

from docx.shared import Pt

doc = Document('C:/Users/Vinny/Desktop/Python/Template.docx')

doc.add_paragraph('Changes:')

doc.add_paragraph('Metrics:')

#add table

table = doc.add_table(rows = 4, cols = 2, style='TableGrid')

doc.save('C:/Users/Vinny/Desktop/Python/rel.docx')

但是它抛出错误为:

Traceback (most recent call last):

File "C:\Users\Vinny\Desktop\Python\abc.py", line 14, in

table = doc.add_table(rows = 4, cols = 2, style='TableGrid')

File "C:\Users\Vinny\AppData\Local\Programs\Python\Python36-32\lib\site-packages\docx\document.py", line 100, in add_table

table.style = style

File "C:\Users\Vinny\AppData\Local\Programs\Python\Python36-32\lib\site-packages\docx\table.py", line 134, in style

style_or_name, WD_STYLE_TYPE.TABLE

File "C:\Users\Vinny\AppData\Local\Programs\Python\Python36-32\lib\site-packages\docx\parts\document.py", line 76, in get_style_id

return self.styles.get_style_id(style_or_name, style_type)

File "C:\Users\Vinny\AppData\Local\Programs\Python\Python36-32\lib\site-packages\docx\styles\styles.py", line 113, in get_style_id

return self._get_style_id_from_name(style_or_name, style_type)

File "C:\Users\Vinny\AppData\Local\Programs\Python\Python36-32\lib\site-packages\docx\styles\styles.py", line 143, in _get_style_id_from_name

return self._get_style_id_from_style(self[style_name], style_type)

File "C:\Users\Vinny\AppData\Local\Programs\Python\Python36-32\lib\site-packages\docx\styles\styles.py", line 57, in __getitem__

raise KeyError("no style with name '%s'" % key)

KeyError: "no style with name 'TableGrid'"

谁能帮我这个忙吗?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值