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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了将变量插入到python-docx生成的单词doc的标题中相关的知识,希望对你有一定的参考价值。

我正在使用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中?

答案

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

document.add_heading(str(currentmonth), 0)

以上是关于将变量插入到python-docx生成的单词doc的标题中的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 python-docx 将复选框表单插入 .docx 文件?

Python-docx - 从 URL 将图片插入 docx

python-docx设置docx文档表格样式

python 操作 word 文档,使用 python-docx 操作 word docx 文档

python-docx 插入点

python用python-docx读写word文档