pythonでwordに画像と表を追加する
15791 ワード
from docx import Document
from docx.enum.text import WD_ALIGN_PARAGRAPH #
from docx.shared import Pt #
from docx.oxml.ns import qn #
from docx.shared import Inches #
import time
today=time.strftime("%Y-%m-%d",time.localtime())
price=100
company_list=[' 1',' 2',' 3',' 4',' 5']
for i in company_list:
document =Document()
document.styles['Normal'].font.name=u' '
document.styles['Normal'].font.size=Pt(14)
#
document.styles['Normal'].element.rPr.rFonts.set(qn('w:eastAsia'),u' ')
#
document.add_picture('1.jpg',width=Inches(6))
p1=document.add_paragraph()
# ,
p1.aligment=WD_ALIGN_PARAGRAPH.CENTER
#
run1=p1.add_run(' %s '%(today))
run1.font.name=' '
run1.font.size=Pt(21) #
#
run1.font.bold=True
# 5
p1.space_after=Pt(5)
## 5
p1.space_before=Pt(5)
p2=document.add_paragraph()
run2=p2.add_run(i+':')
#
run2.font.name=' _GB2312'
run2.element.rPr.rFonts.set(qn('w:eastAsia'),u' _GB2312')
run2.font.size=Pt(16) #
#
run2.font.bold=True
p3=document.add_paragraph()
run3=p3.add_run(' , , %s , 。'%price)
#
run3.font.name=' _GB2312'
run2.element.rPr.rFonts.set(qn('w:eastAsia'),u' _GB2312')
run3.font.size=Pt(16) #
#
table=document.add_table(rows=3,cols=3,style='Table Grid')
#
table.cell(0,0).merge(table.cell(0,2))
# ,
table_run1=table.cell(0,0).paragraphs[0].add_run('XX ')
table_run1.font.name=u' '
table_run1.element.rPr.rFonts.set(qn('w:eastAsia'),u' ')
table.cell(0,0).paragraphs[0].aligment=WD_ALIGN_PARAGRAPH.CENTER
#
table.cell(1,0).text=' '
table.cell(1,1).text=' '
table.cell(1,2).text=' '
table.cell(2,0).text=today
table.cell(2,1).text=str(price)
table.cell(2,2).text=''
#
document.add_page_break()
p5=document.add_paragraph()
run4=p5.add_run(' ')
document.save('%s- .docx'%i)# “ - ”