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)
相关推荐
IDRSolutions_CN12 小时前
如何用Java读取PDF
java·经验分享·pdf·软件工程·团队开发
bu_shuo12 小时前
Edge浏览器PDF字体显示错误
pdf·word·渲染错误
今天也想MK代码13 小时前
zotero pdf中英翻译插件使用
pdf
just a little bit!14 小时前
ORCAD打印pdf
pdf
开开心心就好1 天前
提升办公效率的PDF转图片实用工具
运维·服务器·网络·python·智能手机·pdf·ocr
AI+程序员在路上1 天前
Qt6.8中进行PDF文件读取和编辑
开发语言·qt·pdf
iamBailey2 天前
开源PDF解析工具Marker深度解析
pdf
小汐睡着了2 天前
word导出pdf带有目录导航栏-error记
pdf·word
reasonsummer2 天前
【办公类-99-04】20250504闵豆统计表excle转PDF,合并PDF、添加中文字体页眉+边框下划线
python·pdf·deepseek