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

2024-04-19 17:52:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图通过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'"

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


Tags: nameinfrompyimportidgetstyle