将变量插入 python-docx 生成的 word 文档的标题中

Inserting a variable into a heading of a python-docx generated word doc

我正在使用 python-docx 完成此任务,目前在尝试将变量(即月份)插入到我的 python-docx 生成的 word 文档的标题中时遇到困难。 运行 脚本成功生成了一个 word 文档,但标题打印为 current month 而不是所需的 12(即 Dec)。我的python脚本如下图:

import datetime
import urllib
from docx import Document
from docx.shared import Inches

now = datetime.datetime.now()
currentmonth=now.month
print (currentmonth)

document = Document()

document.add_picture('placeholder.jpg', width=Inches(1.25))

document.add_heading('currentmonth', 0)

p = document.add_paragraph('A plain paragraph having some')
p.add_run('bold').bold = True
p.add_run(' and some ')
p.add_run('italic.').italic = True

document.add_heading('Heading, level 1', level=1)
document.add_paragraph('Intense quote', style='Intense Quote')

document.add_paragraph(
    'first item in unordered list', style='ListBullet'
)
document.add_paragraph(
    'first item in ordered list', style='ListNumber'
)

recordset = [
    {
        "id" : 1,
        "qty": 2,
        "desc": "New item"
    },
    {
        "id" : 2,
        "qty": 2,
        "desc": "New item"
    },
    {
        "id" : 3,
        "qty": 2,
        "desc": "New item"
    },

]

table = document.add_table(rows=1, cols=3)
hdr_cells = table.rows[0].cells
hdr_cells[0].text = 'Qty'
hdr_cells[1].text = 'Id'
hdr_cells[2].text = 'Desc'
for item in recordset:
    row_cells = table.add_row().cells
    row_cells[0].text = str(item["qty"])
    row_cells[1].text = str(item["id"])
    row_cells[2].text = item["desc"]

document.add_page_break()

document.save('demo.docx')

困难出现在**document.add_heading('currentmonth', 0)**。当我删除引号以获得 document.add_heading(currentmont', 0) 时,终端在我 运行 脚本时向我显示错误。错误如下图:

pi@raspberrypi:~/user $ python demo_script_v01.py
12
Traceback (most recent call last):
  File "demo_script_v01.py", line 14, in <module>
    document.add_heading(currentmonth, 0)
  File "/usr/local/lib/python2.7/dist-packages/python_docx-0.8.6-py2.7.egg/docx/document.py", line 43, in add_heading
    return self.add_paragraph(text, style)
  File "/usr/local/lib/python2.7/dist-packages/python_docx-0.8.6-py2.7.egg/docx/document.py", line 63, in add_paragraph
    return self._body.add_paragraph(text, style)
  File "/usr/local/lib/python2.7/dist-packages/python_docx-0.8.6-py2.7.egg/docx/blkcntnr.py", line 36, in add_paragraph
    paragraph.add_run(text)
  File "/usr/local/lib/python2.7/dist-packages/python_docx-0.8.6-py2.7.egg/docx/text/paragraph.py", line 37, in add_run
    run.text = text
  File "/usr/local/lib/python2.7/dist-packages/python_docx-0.8.6-py2.7.egg/docx/text/run.py", line 163, in text
    self._r.text = text
  File "/usr/local/lib/python2.7/dist-packages/python_docx-0.8.6-py2.7.egg/docx/oxml/text/run.py", line 104, in text
    _RunContentAppender.append_to_run_from_text(self, text)
  File "/usr/local/lib/python2.7/dist-packages/python_docx-0.8.6-py2.7.egg/docx/oxml/text/run.py", line 134, in append_to_run_from_text
    appender.add_text(text)
  File "/usr/local/lib/python2.7/dist-packages/python_docx-0.8.6-py2.7.egg/docx/oxml/text/run.py", line 141, in add_text
    for char in text:
TypeError: 'int' object is not iterable

考虑到这个问题,有没有人能解决我如何将当前月份作为变量插入标题中,以便它在 运行 执行此脚本后出现在我的 word 文档中?

add_heading 方法的第一个参数是一个字符串(参见文档 here)。要将 currentmonth(整数)转换为字符串,只需使用 str(currentmonth),如下所示:

document.add_heading(str(currentmonth), 0)