我无法使用 python 3.4 将自定义表格样式应用于 WORD 文档。我遵循了@scanny 的一个很好的方法:我创建了一个空白的 WORD 文档,创建了一个自定义表格样式并将其保存为“或”。我创建了一个新表格并将自定义表格样式应用于它。然后我删除了表格并将文档保存为“template.docx。然后我按如下方式应用了样式:
document = Document("C:/pytest/template.docx")
table = document.add_table(rows=1, cols=3)
table.style = "OR"
hdr_cells = table.rows[0].cells
hdr_cells[0].paragraphs[0].add_run('Date Filmed:').bold = True
hdr_cells[2].paragraphs[0].add_run('Barcode Number:').bold = True
row_cells = table.add_row().cells
row_cells[0].text = date
row_cells[2].text = bcode
Run Code Online (Sandbox Code Playgroud)
当我运行程序时,它得到一个错误:文件 "C:\Python34\lib\site-packages\python_docx-0.8.5-py3.4.egg\docx\styles\styles.py", line 57, in getitem raise KeyError("没有名为 '%s' 的样式" % key) KeyError: "没有名为 'Title' 的样式"
它不是在代码中指定一行,而是一个名为“styles.py”的文件。我的语法错误吗?
谢谢,杰夫
我试图打开现有的Excel 2013文件,添加数据,然后保存(同名),然后关闭它,然后关闭Excel.代码将打开文件,选择正确的工作表并写入数据,但是当我尝试保存它时,我得到属性错误.我错过了图书馆吗?这是代码:
import win32com.client as win32
def Inventory_Status():
excel = win32.gencache.EnsureDispatch('Excel.Application') # opens Excel
wb = excel.Workbooks.Open(r'C:/pytest/Test.xlsx') # opens "Test" file
wb.Sheets(2).Select() # select 2nd worksheet "Aisle_2"
excel.Visible = True
excel.Range("A1").Select()
excel.ActiveCell.Value = "1234" # Fill in test data #
wb.save()
wb.Close()
excel.Quit()
Inventory_Status()
raise AttributeError("'%s' object has no attribute '%s'" % (repr(self), attr))
AttributeError: '<win32com.gen_py.Microsoft Excel 15.0 Object Library._Workbook instance at 0x5901424>' object has no attribute 'save'
Run Code Online (Sandbox Code Playgroud)