表格样式键错误:u“没有名为'table Grid'的样式”

用户

我试图通过pythondocx插入一个表,但是它给出了错误 代码是:

#-*-coding:utf-8-*-
import re 
import time
import datetime
import sys
import os
import csv
from docx import Document
import docx
from docx import *  
from docx.oxml import OxmlElement
from docx.oxml.ns import qn
from docx import Document
from docx.shared import Inches
from docx.enum.text import WD_ALIGN_PARAGRAPH
from docx.shared import Pt
from docx.shared import Cm
from docx import section
from docx.enum.table import WD_TABLE_ALIGNMENT
import win32com.client

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,'logsample.docx')
doc=Document(docxFilePath)



def buildtable():


    #id_numbers=sum(1 for row in idlist)

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


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







buildtable()    

它总是给我的错误是:

Traceback (most recent call last): File "C:\Users\Robin-work-laptop\Desktop\New folder\log\makereport.py", line 49, in buildtable() File "C:\Users\Robin-work-laptop\Desktop\New folder\log\makereport.py", line 41, in buildtable table.style = 'Table Grid' 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'"

请帮助我阅读文档样式在列表中作为表格网格


已被浏览了5792次
更新日期: 2020-10-28 19:44:51
1 个回答
fefe Tyson

文档中未定义“表格网格”样式。Word中看似“内置”的样式实际上在第一次使用之前不会添加到文档中。在

您可以通过阅读python-pptx文档的相关部分来了解这些所谓的“潜在样式”的行为:
http://python-docx.readthedocs.io/en/latest/user/styles-understanding.html
这里:
http://python-docx.readthedocs.io/en/latest/user/styles-using.html

评论 - 2020年9月11日 16:31

最新Python问答

推荐Python问答