80869

table style KeyError: u“no style with name 'Table Grid'”

I trying to insert a table via python-docx but it give the error code is:

#-*-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()

it always gives me error is :

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 <strong>getitem</strong> raise KeyError("no style with name '%s'" % key) KeyError: u"no style with name 'Table Grid'"

Please help I read the documnets the style is in the list as Table Grid

Answer1:

Your document does not have the 'Table Grid' style defined in it. The styles that appear to be "built-in" in Word are actually not added to the document until the first time they are used.

You can learn more about the behavior of these so-called "latent styles" by reading the related part of the python-pptx documentation here: http://python-docx.readthedocs.io/en/latest/user/styles-understanding.html and here: http://python-docx.readthedocs.io/en/latest/user/styles-using.html

Recommend

  • Refactoring require_once file in a project
  • Perl chdir doesn't work
  • django-pipeline DEBUG=True, compressed file not found
  • subprocess.Popen: 'OSError: [Errno 13] Permission denied' only on Linux
  • Django-ckeditor file upload doesn't work
  • Separate float into digits
  • Python-3 Request parameter encoding error
  • TypeError: Main.login is not a function
  • python struct.pack(): pack multiple datas in a list or a tuple
  • Get highlight text in current window and send it in a popup
  • Getting directory of input file (Applescript)
  • How to click on a link that has a certain content in puppeteer?
  • Is it possible to make imports depend on the location of my Lua script instead of the current direct
  • Pointer to a logger class provide for all other classes? [duplicate]
  • Send the string to its hex equivalent
  • How to remove the dot in to_char if the number is an integer
  • Call a macro with parameters : Python win32com API
  • Python: Split a String Field into 3 Separate Fields using Lambda
  • how do you obtain the address of an instance after overriding the __str__ method in python
  • date format change with DT and shiny
  • removing last 3 characters on a file (file extension)
  • C function strchr - How to calculate the position of the character?
  • Bad interaction between Zope2 XML-RPC and AT Image mutator?
  • Getting short path in python
  • How to read piped content in C?
  • How to access culture data in globalize.js V1.0.0
  • Parse a date string in a specific locale (not timezone!)
  • Date Conversion from yyyy-mm-dd to dd-mm-yyyy
  • print() is showing quotation marks in results
  • script to move all files from one location to another location
  • ILMerge & Keep Assembly Name
  • How to handle AllServersUnavailable Exception
  • Symfony2: How to get request parameter
  • Apache 2.4 - remove | delete | uninstall
  • 0x202A in filename: Why?
  • Run Powershell script from inside other Powershell script with dynamic redirection to file
  • Cannot Parse HTML Data Using Android / JSOUP
  • How do you join a server to an Active Directory (domain)?
  • Understanding cpu registers
  • Python/Django TangoWithDjango Models and Databases