我想用 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'"