wrod生成pdf。[特殊字符]改背景

import subprocess

import os,time

from rembg import remove, new_session

from PIL import Image

import io

from docxtpl import DocxTemplate, InlineImage

from docx.shared import Inches

input_folder = 'tupian' # 输入文件夹

kouchu_folder = 'kouchu' # 去背景图像保存文件夹

output_folder = 'dangan' # 合成后的输出文件夹

background_folder = 'beijing' # 背景文件夹

model_path = "mode/u2net.onnx" # 模型路径

wendang_path = "wendang" # 模型路径

pdf_path = "pdf" # 模型路径

doc = DocxTemplate("moban.docx")

context = {

'company_name': "World company",

}

如果输出文件夹不存在,则创建它

if not os.path.exists(output_folder):

os.makedirs(output_folder)

获取背景文件夹中的所有背景图片

background_images = f for f in os.listdir(background_folder) if f.lower().endswith(('.png', '.jpg', '.jpeg'))

Function to convert docx to PDF

def convert_to_pdf(docx_path, pdf_output_path):

subprocess.run("/Applications/LibreOffice.app/Contents/MacOS/soffice", "--headless", "--convert-to", "pdf", docx_path, "--outdir", pdf_output_path)

遍历输入文件夹中的所有图片文件

for filename in os.listdir(input_folder):

input_path = os.path.join(input_folder, filename)

复制代码
# 确保文件是图片文件(你可以根据需要调整此条件)
if filename.lower().endswith(('.png', '.jpg', '.jpeg')):
    output_path = os.path.join(kouchu_folder, filename)
    # 读取输入图片并去背景
    with open(input_path, 'rb') as input_file:
        input_data = input_file.read()
        session = new_session(model_name='u2net_custom', model_path=model_path)
        output_data = remove(input_data, session=session)

    # 将去除背景后的数据转为图片
    input_image = Image.open(io.BytesIO(output_data))
    with open(output_path, 'wb') as output_file:
        output_file.write(output_data)

    # 遍历背景文件夹中的所有背景图像
    for bg_filename in background_images:
        background_path = os.path.join(background_folder, bg_filename)
        background_image = Image.open(background_path)
        

        # 调整背景大小,使其与输入图像大小匹配
        background_image = background_image.resize(input_image.size)

        # 合成图像:将去背景后的图片合成到背景上
        background_image = background_image.convert("RGBA")
        input_image = input_image.convert("RGBA")

        # 合成图像(将前景图像叠加到背景上)
        combined_image = Image.alpha_composite(background_image, input_image)

        # 将合成后的图像转换为RGB模式并保存
        combined_image = combined_image.convert("RGB")

        # 创建输出路径,根据背景文件名生成不同的输出文件名
        output_filename = f"{os.path.splitext(filename)[0]}_{os.path.splitext(bg_filename)[0]}.jpg"
        output_path = os.path.join(output_folder, output_filename)

        # 保存合成后的图片
        combined_image.save(output_path)
        print(f"处理并保存了 {filename} 与 {bg_filename} 合成的图片 到 {output_path}")
        context['company_logo'] = InlineImage(doc, output_path, width=Inches(2))  # 插入图片并设置宽度
        print(context)
        # 渲染模板
        doc.render(context)
        # 保存生成的文档
        doc_output_path = os.path.join(wendang_path, f"generated_doc_{os.path.splitext(filename)[0]}_{os.path.splitext(bg_filename)[0]}.docx")
        doc.save(doc_output_path)
        # 延迟2秒
        time.sleep(2)
        pdf_output_path = os.path.join(pdf_path,f"generated_doc_{os.path.splitext(filename)[0]}_{os.path.splitext(bg_filename)[0]}.pdf")
        convert_to_pdf(doc_output_path, pdf_output_path)
相关推荐
爱昏羔10 小时前
上篇:从PDF到向量库 — 物流行业RAG系统的知识库构建全解析
python·langchain·pdf·agent·rag
AI导出鸭PC端13 小时前
手机deepseek怎么导出pdf AI导出鸭
人工智能·pdf
蓝创工坊Blue Foundry1 天前
PaddleOCR 本地部署教程:小模型 OCR 如何完成字段提取到 Excel
pdf·自动化·ocr·excel·paddlepaddle·paddle
蓝创工坊Blue Foundry2 天前
个人藏书太多怎么整理?用 OCR 字段提取汇总成电子书目
pdf·ocr·excel·文心一言·paddlepaddle·paddle
蓝创工坊Blue Foundry3 天前
扫描件批量转 Excel:先确认要整表还原还是字段汇总
python·pdf·ocr·excel
审小匠OpenCPAi3 天前
审计票据与财报 PDF 怎么识别?通用 OCR、表格结构识别与多模态大模型的精度对比
pdf·ocr·审计
2501_930707783 天前
使用C#代码将 PDF 文件转换为 Markdown 格式
pdf
大白要努力!3 天前
纯前端实现 PDF 加水印工具 —— 零后端、支持中文、实时预览
前端·pdf·html
Android洋芋4 天前
PrintFriendly网页文章转PDF插件工具及技术分析
运维·服务器·pdf·网页转pdf
Kari114 天前
连锁门店开业网络验收怎么做:把 PDF 清单改造成可回放的 Skill
网络·人工智能·pdf·php