robinshion 2018-06-04 22:58 采纳率: 0%
浏览 3374
已结题

python-docx no style with name 'Table Grid'" 求帮助

我想用 python-docx 新添一个表格但是总是出错。
代码是

 #-*-coding:utf-8-*-
import re 
import time
import datetime
import sys
import os
import csv
import docx

from docx import Document
from docx import *  
from docx.enum.text import WD_ALIGN_PARAGRAPH
from docx.enum.table import WD_TABLE_ALIGNMENT
from docx.enum.style import WD_STYLE_TYPE


lastday=str(datetime.date.today()-datetime.timedelta(days=1))

CURRENT_DIR = os.path.dirname(os.path.abspath(sys.argv[0]))

docxFilePath = os.path.join(CURRENT_DIR,'logtemp.docx')

doc=Document(docxFilePath)

def buildtable():


    #id_numbers=sum(1 for row in idlist)

    table = doc.add_table(rows = 3, cols = 2)
    print type(table)

    table.alignment = WD_TABLE_ALIGNMENT.CENTER
    table.style = 'Table Grid'



buildtable()    

但是编译的时候会出现一下错误

Traceback (most recent call last):
File "C:\Users\Robin-work-laptop\Dropbox\dot entrance log\PCsite\makereport.py", line 79, in
buildtable()
File "C:\Users\Robin-work-laptop\Dropbox\dot entrance log\PCsite\makereport.py", line 65, in buildtable
table=doc.add_table(rows=9,cols=5,style = 'Table Grid')
File "C:\Python27\lib\site-packages\docx\document.py", line 100, in add_table
table.style = style
File "C:\Python27\lib\site-packages\docx\table.py", line 134, in style
style_or_name, WD_STYLE_TYPE.TABLE
File "C:\Python27\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:\Python27\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:\Python27\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:\Python27\lib\site-packages\docx\styles\styles.py", line 57, in getitem
raise KeyError("no style with name '%s'" % key)
KeyError: u"no style with name 'Table Grid'"

  • 写回答

4条回答

  • dabocaiqq 2018-06-05 03:31
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘