私はこのタスクにpython-docxを使用していますが、現在はpython-docxの見出しに変数(すなわち月)を挿入しようとしています。 word doc。スクリプトを実行するとdocという単語が正常に生成されますが、見出しは12
(つまりDec)の代わりにcurrent month
と表示されます。私のpythonスクリプトは以下の通りです。python-docxで生成された単語の見出しに変数を挿入するdoc
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)
という引用符を削除すると、スクリプトを実行すると端末にエラーが表示されます。エラーを以下に示します。それは、このスクリプトを実行した後、私のWord文書に表示されるように、この問題を考える
[email protected]:~/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
は、いずれかが、私は見出しに変数として現在の月を挿入することができますどのように解決策を持っていません?