python自动化文档(Word、Excel、PPT)

本文介绍了如何使用Python的`python-docx`和`docxtpl`库来创建和更新Word文档,以及利用模板创建Excel和PPT。涵盖了从基本的DataFrame转换为Word表格,到通过模板动态填充内容和修改文档的详细步骤。
摘要由CSDN通过智能技术生成

利用python自动化生成Word、Excel、PPT等各类文档报表

1、Word

若是创建新word文档,建议使用 python-docx,若想通过已有格式的word模板文档来更新文档建议使用 docxtpl

1.1 python-docx

1.1.1 创建word文档:
import pandas as pd
from docx import Document


def get_table(df):
    """将Dataframe生成table"""
    table = document.add_table(
        rows=1, cols=len(df.columns), style="Table Grid"
    )
    hdr_cells = table.rows[0].cells
    for i in range(len(df.columns)):
        hdr_cells[i].text = u"%s" % df.columns[i]  
    for i in range(len(df.index)):
        row_cells = table.add_row().cells
        for j in range(len(df.columns)):
            row_cells[j].text = str(df.iloc[i,j])

document = Document()   
document.add_heading(u"文档标题", 0)
document.add_heading(u"一级标题", level=1)
document.add_paragraph(u"添加段落")
get_table(pd.DataFrame({"col": list("abc")}))
document.add_picture(r'图片路径', width=Inches(6))  # 添加图片
document.add_page_break()  # 换页
document.save(r"文档保存.doc")
1.1.2 更新word文档,对已设置好格式的word文档中的指定内容进行更新:
from docx import Document


def replace_content(replace_obj, new_str):
	"""文本替换"""
    if isinstance(replace_obj, docx.text.paragraph.Paragraph):
        replace_obj.clear()
        replace_obj.add_run(new_str)
    else:
        replace_obj.paragraphs[0].clear()
        replace_obj.paragraphs[0].add_run(new_str)

doc = docx.Document("template.doc")
replace_content(doc.paragraphs[2], "嘿嘿")  # 指定段落替换
table = doc.tables[0]  # 获取文档中表格
replace_content(table.cell(0, 2), "哈哈")  # 指定单元格内容替换
doc.save("new.doc")

1.2 docxtpl

模板文档 template.docx
在这里插入图片描述
代码样例:

from docxtpl import DocxTemplate

doc = DocxTemplate("template.docx")
context = {
    "paragraph": "嘿嘿哈哈呵呵"*20,
    "table_name": "人员表",
    "persons": [
        {"name": "小明", "age": 88, "tel": 111},
        {"name": "小红", "age": 18, "tel": 112},
        {"name": "小黄", "age": 20, "tel": 111}
    ]
}
doc.render(context)
doc.save("生成文档.docx")

生成文档展示:
在这里插入图片描述

Excel

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值