Enseñarle cómo usar Python para manipular Word y escribir automáticamente un informe de renuncia.

Prefacio

Hoy compartiré con ustedes un artículo de automatización de Python.

Python es una buena mano para el procesamiento automatizado, como escribir automáticamente aplicaciones de renuncia. ! !

imagen

instalación

pip install python-docx -i https://pypi.douban.com/simple/

Uso básico

Crea un documento docx

from docx import Document
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
from docx.oxml.ns import qn
from docx.shared import RGBColor, Pt

# 初始化一个文档
document = Document()

# 加个标题
paragraph = document.add_heading('开空调', level=3)

document.save('开空调.docx')

Agregar un párrafo

# LEFT      =>  左对齐
# CENTER    =>  文字居中
# RIGHT     =>  右对齐
# JUSTIFY   =>  文本两端对齐

paragraph = document.add_paragraph("前几天最热的时候我们家热得快炸了!!!")
# 字体格式设置
paragraph_format = paragraph.paragraph_format
paragraph_format.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
paragraph.add_run('粗体').bold = True

Agregar una imagen

from docx.shared import Inches, RGBColor

paragraph = document.add_paragraph()
# 图片居中设置
paragraph.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
run = paragraph.add_run("")
run.add_picture('./ico.jpg', width=Inches(1.0), height=Inches(1.0))

Establecer el color y el tamaño de la fuente

paragraph = document.add_paragraph()
paragraph.add_run('那你开')
paragraph.add_run('空调').font.color.rgb = RGBColor(54, 95, 145)
paragraph.add_run('啊!!')
paragraph.add_run('开空调!!').font.size = Pt(30)  # 字体大小设置,和word里面的字号相对应

Insertar tabla

document.add_page_break()
# 创建表格
table = document.add_table(rows=2, cols=2)
table.style = 'LightShading-Accent1'
cell = table.cell(0, 1)
cell.text = '这是第一行第二列的单元格'

row = table.rows[1]
row.cells[0].text = '第二行第一列'
row.cells[1].text = '第二行第二列'

Comience a compilar un informe de renuncia

# -*- coding: utf-8 -*-
"""
@Time :2021/2/1 10:51
@Auth :wutong
@File :自动离职报告.py
@IDE :PyCharm
"""
from docx import Document
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
from docx.oxml.ns import qn
from docx.shared import RGBColor, Pt
from docx.shared import Inches, RGBColor
from lzStr import lzStr

# todo 初始化一个文档
document = Document()
# 全局指定字体
document.styles['Normal'].font.name = u'.萍方-简'
document.styles['Normal']._element.rPr.rFonts.set(qn('w:eastAsia'), u'.萍方-简')

# todo 加个标题
paragraph = document.add_heading('离职申请', level=3)

# todo 居中
paragraph_format = paragraph.paragraph_format
paragraph_format.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER

paragraph = document.add_paragraph()
paragraph.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
run = paragraph.add_run("")
run.add_picture('./image.jpg', width=Inches(1.0), height=Inches(1.0))

paragraph = document.add_paragraph()
lz_add_run = paragraph.add_run(lzStr.format(name="吴老板"))
lz_add_run.font.size = Pt(8)  # 字体大小设置,和word里面的字号相对应
lz_add_run.font.color.rgb = RGBColor(54, 95, 145)

document.save('离职.docx')

Archivo de plantilla (lzStr.py)

# -*- coding: utf-8 -*-
"""
@Time :2021/2/1 10:53
@Auth :wutong
@File :lz.py
@IDE :PyCharm
"""

"""此模板来自网络,做了小小改动 !!!"""

lzStr = '''\t\t你好啊! 我({name})已决定于2050年4月15日正式离职,\n
\t\t所以特提前半月向您提交这份辞职申请。\n
\t\t还请留意一下简历中是否有适合接替我工作的人选,\n
\t\t感激你这几年对我的照顾和帮忙,十分感激!\n
\t\t最近在整理自我的简历,突然发觉这几年我在公司做的工种实在太多了,\n
\t\t细数一下,有不下十种。\n
\t\t先是1900年3月从前台开始做起,\n
\t\t然后是人事,行政,市场开拓,婚庆,护肤品牌信息收集,再到之后的外贸。\n
\t\t还有兼职文案,校对,送货,以及各种各样的手工。\n
\t\t并且大多数时候我同时在做好几件事情,\n
\t\t虽然我从来没有抱怨过我的工作太多可是这并不表示我的工作量小。\n
\t\t并且我所有的工作都有做完的那天,或者是被人接手的那一天,\n
\t\t这也就意味着一个新的工作即将来临。\n
\t\t有时候就觉得自我是个高级打杂工,真的太杂了,\n
\t\t杂到我此刻已经搞不清楚我自我能干什么,想干什么,\n
\t\t我此刻对自我的职业定位和前程也是一片迷茫。\n
\t\t所以,我此刻想休息一下,为自我的将来好好打算一下,\n
\t\t重新规划自我的职业和人生。\n\n

\t\t记得年前已经跟你谈过一次,谈过我的想法,\n
\t\t所以想必我的辞职对你来说应当不会太意外。\n
\t\t我是公司刚起步不久就过来帮你创业了,\n
\t\t到此刻为止已经做了快3年多了,\n
\t\t看着公司一天天成长壮大,慢慢的走上正轨我心里也是蛮高兴的。\n
\t\t老实说离开公司多少还是有些不舍。我明白你的想法点子也很多,\n
\t\t以前每一次你有新想法我都很支持你,\n
\t\t都会全心全意按着你的想法去做事。\n
\t\t公司出现了什么问题,我都会尽我最大的努力去帮你处理好\n
\t\t因为我很重承诺,我答应的事情必须会做到。\n\n

\t\t此刻公司已经走上正轨并且新鲜血液不断供给,\n
\t\t我想我能够激流勇退了,也请你给我一次机会,\n
\t\t一次让我实现自我想法的机会。\n

\t\t{name}\t2050-04-15 04:15:15\n
'''

Después de que se ejecute el programa, se generará un archivo llamado "Resignation.docx" en el directorio actual del código, y el contenido del informe de renuncia generado es el siguiente:

imagen

La imagen de arriba es autodefinible, debe colocarse en el mismo directorio que el archivo de código, llamado image.jpg.

Bueno, ¡puedes aprovecharlo para presentar tu renuncia! ! ! (Te lo digo en voz baja, ahora todas las solicitudes se envían con DingTalk, nadie imprimirá la versión en papel jajajaja)

El editor de código ha sido empaquetado. Aquellos que necesiten el código de proyecto de este artículo, presten atención al editor y soliciten el grupo QQ: 721195303 gratis

Supongo que te gusta

Origin blog.csdn.net/aaahtml/article/details/114277569
Recomendado
Clasificación