import os
os.chdir('D:\\python_major\\auto_office14')
from docx import Document
doc = Document()
doc.add_heading('一级标题', level = 1)
paragraph1 = doc.add_paragraph('这是一个段落')
paragraph2 = doc.add_paragraph('这又是一个段落')
paragraph3 = doc.add_paragraph()
paragraph3.add_run('加粗').bold = True
paragraph3.add_run('普通')
paragraph3.add_run('斜体').italic = True
doc.add_page_break()
from docx.shared import Cm
doc.add_picture('demo.png')
doc.add_picture('demo.png', width = Cm(5), height = Cm(3))
只给一个宽度或高度,另一个的尺寸会自动计算
records = [
['学号','姓名','成绩'],
[101,'李雷',95],
[102,'韩梅梅',100],
[103,'马冬梅',98]
]
table = doc.add_table(rows = 4, cols = 3)
for row in range(4):
cells = table.rows[row].cells
for col in range(3):
cells[col].text = str(records[row][rol])
全部代码如下
import os
os.chdir('D:\\python_major\\auto_office14')
from docx import Document
doc = Document()
doc.add_heading('一级标题', level = 1)
paragraph1 = doc.add_paragraph('这是一个段落')
paragraph2 = doc.add_paragraph('这又是一个段落')
paragraph3 = doc.add_paragraph()
paragraph3.add_run('加粗').bold = True
paragraph3.add_run('普通')
paragraph3.add_run('斜体').italic = True
from docx.shared import Cm
doc.add_picture('demo.png')
doc.add_picture('demo.png', width = Cm(5), height = Cm(3))
records = [
['学号','姓名','成绩'],
[101,'李雷',95],
[102,'韩梅梅',100],
[103,'马冬梅',98]
]
table = doc.add_table(rows = 4, cols = 3)
for row in range(4):
cells = table.rows[row].cells
for col in range(3):
cells[col].text = str(records[row][col])
doc.save('demo.docx')
编写一个Python程序,要求
(1)制作一个自动生成请假条的Word文档的程序
(2)通过命令行提示用户输入姓名、请假原因
(3)自动生成带标题、正文内容的请假条
(4)日期自动填写今天日期
首先扩充一个知识点:format格式化中槽的使用,举个栗子,有时候我们需要将多个变量在一句话中显示,但是如果全是使用print()语句进行输出的话,会显得特别多余,代码看上去很不简洁,因此这里就可以使用format方法进行格式化输出
name = input("请输入请假人姓名:")
division = input("请输入所属部门:")
reason = input("请输入请假事由:")
day = input('请输入请假天数:')
detailed_time = input("请输入请假的具体时间:")
print("本人:" + name + "," + "所属部门:" + division + "," + "因:" + reason + "," + "需请假" + day + "天" + "," + "请假具体时间为:" + detailed_time)
通过demo1的代码可以发现,最后的print()语句过于冗肿,单纯的靠着字符进行拼接,代码不够简洁,因此可以使用format方法进行格式化输出
name = input("请输入请假人姓名:")
division = input("请输入所属部门:")
reason = input("请输入请假事由:")
day = input('请输入请假天数:')
detailed_time = input("请输入请假的具体时间:")
print("本人:{},所属部门:{},因{},需请假{}天,请假具体时间为:{}".format(name,division,reason,day,detailed_time))
代码运行后输入相关的信息输出结果为:
对比输出的结果可以发现,两者输出的结果是一致的,但是demo2的输出代码要比demo1要简洁的多,这里的{}就叫做“槽”,用来装变量的,有几个“槽”format后的()里面就要有几个变量,而且默认是按照变量的顺序来填充这个“槽”的
关于“槽”内部的参数设定,可以直接用一个思维导图来说明,如下
比如现在想对这个请假条的填写部分进行完善,想要在输入的数字下面加入下划线,而且要求下划线的长度不得大于输出字符的4个长度,就可以进行如下的修改(需要使用到占位符和槽内加槽)
name = input("请输入请假人姓名:")
division = input("请输入所属部门:")
reason = input("请输入请假事由:")
day = input('请输入请假天数:')
detailed_time = input("请输入请假的具体时间:")
content = "本人{0:_^{1}},所属部门{2:_^{3}},因{4:_^{5}},需请假{6:_^{7}}天,请假具体时间为{8:_^{9}}".format(
name,len(name) + 2,division,len(division) + 2,reason,len(reason) + 2,day,len(day) + 2,detailed_time,len(detailed_time) + 2)
print(content)
import os
os.chdir('D:\\python_major\\auto_office14')
from docx import Document
import datetime
doc = Document()
doc.add_heading('请假条',level = 1)
name = input("请输入请假人姓名:")
division = input("请输入所属部门:")
reason = input("请输入请假事由:")
day = input('请输入请假天数:')
detailed_time = input("请输入请假的具体时间:")
content = "本人{0:_^{1}},所属部门{2:_^{3}},因{4:_^{5}},需请假{6:_^{7}}天,请假具体时间为{8:_^{9}}".format(
name,len(name) + 4,division,len(division) + 4,reason,len(reason) + 4,day,len(day) + 2,detailed_time,len(detailed_time) + 4)
today_year = datetime.datetime.today().year
today_month = datetime.datetime.today().month
today_day = datetime.datetime.today().day
sign_date = "{}年{}月{}日".format(today_year,today_month,today_day)
paragraph = doc.add_paragraph(content)
paragraph1 = doc.add_paragraph(name)
paragraph2 = doc.add_paragraph(sign_date)
doc.save("综合应用.docx")
print("-"*30)
print('\n\n请假条自动生成完毕,文件的路径在:{}'.format(os.getcwd()))
import os
os.chdir('D:\\python_major\\auto_office14')
import datetime
from docx import Document
from docx.enum.text import WD_ALIGN_PARAGRAPH
from docx.shared import Pt, RGBColor
from docx.oxml.ns import qn
该部分代码完成的功能是:设置程序运行路径、导入相关的库
标题一般是居中,这里设置的是一级标题,默认样式
doc = Document()
heading_1 = '请 假 条'
paragraph_1 = doc.add_heading(heading_1, level = 1)
paragraph_1.alignment = WD_ALIGN_PARAGRAPH.CENTER
一般正文的第一段是称呼
greeting_word = '尊敬的领导:'
paragraph_2 = doc.add_paragraph(greeting_word)
name = input("请输入请假人姓名:")
division = input("请输入所属部门:")
reason = input("请输入请假事由:")
day = input('请输入请假天数:')
detailed_time = input("请输入请假的具体时间:")
word_1 = " 本人"
word_2 = ",所在部门"
word_3 = ",由于"
word_4 = ",需请假"
word_5 = "天,请假的具体时间为"
word_6 = "。"
paragraph_3 = doc.add_paragraph()
paragraph_3.add_run(word_1)
paragraph_3.add_run("{0: ^{1}}".format(name,len(name) + 6)).underline = True
paragraph_3.add_run(word_2)
paragraph_3.add_run("{0: ^{1}}".format(division,len(division) + 6)).underline = True
paragraph_3.add_run(word_3)
paragraph_3.add_run("{0: ^{1}}".format(reason,len(reason) + 6)).underline = True
paragraph_3.add_run(word_4)
paragraph_3.add_run("{0: ^{1}}".format(day,len(day) + 6)).underline = True
paragraph_3.add_run(word_5)
paragraph_3.add_run("{0: ^{1}}".format(detailed_time,len(detailed_time) + 6)).underline = True
paragraph_3.add_run(word_6)
paragraph_3.paragraph_format.line_spacing = 1.5
word_7 = '申请人:'
paragraph_4 = doc.add_paragraph()
paragraph_4.add_run(word_7)
paragraph_4.add_run("{0: ^{1}}".format(name,len(name) + 6)).underline = True
paragraph_4.alignment = WD_ALIGN_PARAGRAPH.RIGHT
word_8 = '日期:'
today_year = datetime.datetime.today().year
today_month = datetime.datetime.today().month
today_day = datetime.datetime.today().day
sign_date = "{}年{}月{}日".format(today_year,today_month,today_day)
paragraph_5 = doc.add_paragraph()
paragraph_5.add_run(word_8)
paragraph_5.add_run("{0: ^{1}}".format(sign_date,len(sign_date) + 6)).underline = True
paragraph_5.alignment = WD_ALIGN_PARAGRAPH.RIGHT
for paragraph in doc.paragraphs:
for run in paragraph.runs:
run.font.color.rgb = RGBColor(0,0,0)
run.font.name = '微软雅黑'
r = run._element.rPr.rFonts
r.set(qn('w:eastAsia'),'微软雅黑')
doc.save("{}-请假条.docx".format(name))
print("-"*30)
print("\n{}的请假条已经生成\n\n文件位置在:{}\n\n文件名称为:{}-请假条.docx".format(name,os.getcwd(),name))
import os
os.chdir('D:\\python_major\\auto_office14')
import datetime
from docx import Document
from docx.enum.text import WD_ALIGN_PARAGRAPH
from docx.shared import Pt, RGBColor
from docx.oxml.ns import qn
doc = Document()
heading_1 = '请 假 条'
paragraph_1 = doc.add_heading(heading_1, level = 1)
paragraph_1.alignment = WD_ALIGN_PARAGRAPH.CENTER
greeting_word = '尊敬的领导:'
paragraph_2 = doc.add_paragraph(greeting_word)
name = input("请输入请假人姓名:")
division = input("请输入所属部门:")
reason = input("请输入请假事由:")
day = input('请输入请假天数:')
detailed_time = input("请输入请假的具体时间:")
word_1 = " 本人"
word_2 = ",所在部门"
word_3 = ",由于"
word_4 = ",需请假"
word_5 = "天,请假的具体时间为"
word_6 = "。"
paragraph_3 = doc.add_paragraph()
paragraph_3.add_run(word_1)
paragraph_3.add_run("{0: ^{1}}".format(name,len(name) + 6)).underline = True
paragraph_3.add_run(word_2)
paragraph_3.add_run("{0: ^{1}}".format(division,len(division) + 6)).underline = True
paragraph_3.add_run(word_3)
paragraph_3.add_run("{0: ^{1}}".format(reason,len(reason) + 6)).underline = True
paragraph_3.add_run(word_4)
paragraph_3.add_run("{0: ^{1}}".format(day,len(day) + 6)).underline = True
paragraph_3.add_run(word_5)
paragraph_3.add_run("{0: ^{1}}".format(detailed_time,len(detailed_time) + 6)).underline = True
paragraph_3.add_run(word_6)
paragraph_3.paragraph_format.line_spacing = 1.5
word_7 = '申请人:'
paragraph_4 = doc.add_paragraph()
paragraph_4.add_run(word_7)
paragraph_4.add_run("{0: ^{1}}".format(name,len(name) + 6)).underline = True
paragraph_4.alignment = WD_ALIGN_PARAGRAPH.RIGHT
word_8 = '日期:'
today_year = datetime.datetime.today().year
today_month = datetime.datetime.today().month
today_day = datetime.datetime.today().day
sign_date = "{}年{}月{}日".format(today_year,today_month,today_day)
paragraph_5 = doc.add_paragraph()
paragraph_5.add_run(word_8)
paragraph_5.add_run("{0: ^{1}}".format(sign_date,len(sign_date) + 6)).underline = True
paragraph_5.alignment = WD_ALIGN_PARAGRAPH.RIGHT
for paragraph in doc.paragraphs:
for run in paragraph.runs:
run.font.color.rgb = RGBColor(0,0,0)
run.font.name = '微软雅黑'
r = run._element.rPr.rFonts
r.set(qn('w:eastAsia'),'微软雅黑')
doc.save("{}-请假条.docx".format(name))
print("-"*30)
print("\n{}的请假条已经生成\n\n文件位置在:{}\n\n文件名称为:{}-请假条.docx".format(name,os.getcwd(),name))