将变量插入到python-docx生成的单词doc的标题中

问题描述 投票:0回答:1

我正在使用python-docx执行此任务,并且我目前在尝试将变量(即月份)插入到我的python-docx生成的单词doc的标题中时遇到了困难。成功运行脚本会生成一个单词doc,但标题打印为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 doc中?

python datetime variables raspberry-pi python-docx
1个回答
2
投票

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

document.add_heading(str(currentmonth), 0)
© www.soinside.com 2019 - 2024. All rights reserved.