惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - Franky
Apple Machine Learning Research
Apple Machine Learning Research
月光博客
月光博客
Vercel News
Vercel News
Recent Announcements
Recent Announcements
B
Blog RSS Feed
Y
Y Combinator Blog
M
MIT News - Artificial intelligence
MongoDB | Blog
MongoDB | Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
雷峰网
雷峰网
D
Docker
Jina AI
Jina AI
IT之家
IT之家
人人都是产品经理
人人都是产品经理
L
LangChain Blog
G
Google Developers Blog
Google DeepMind News
Google DeepMind News
MyScale Blog
MyScale Blog
博客园 - 叶小钗
The GitHub Blog
The GitHub Blog
The Cloudflare Blog
A
About on SuperTechFans
Hugging Face - Blog
Hugging Face - Blog

博客园 - daviyoung

Windows 11 22H2 安装 .NET Framework 3.5 完整教程 System.Threading.Timer 详细讲解 Agent 开发入门(一):从零构建你的第一个智能体 用 C# 开发一个解释器语言——基于《Crafting Interpreters》的实战系列(五)表达式求值 python使用plotly绘制图表 手把手搭建OPC UA服务器 图像处理库Pillow的使用:批量裁剪图片 modbus(二)用NModbus4库实现Modbus tcp从站 Jenkins 容器化实践:Docker 部署与 CI/CD 流水线配置 Streamlit实战 用pycdc批量反编译pyc文件 以ENS 的 BaseRegistrarImplementation 合约为例,用web3.py调用合约 虚拟环境下安装包后,vs code仍然有下滑波浪线及显示找不到包(运行是正常的)的解决办法 Merkle Tree 用 C# 开发一个解释器语言——基于《Crafting Interpreters》的实战系列(四)可视化 语法树 Solidity开发ERC20智能合约claim token的功能 Solidity开发ERC20智能合约demo及部署到测试网 用 C# 开发一个解释器语言——基于《Crafting Interpreters》的实战系列(三)表达式的抽象语法树设计(Expr) 用 C# 开发一个解释器语言——基于《Crafting Interpreters》的实战系列(二)词法分析器
python-docx库的使用:图片插入到word文档里
daviyoung · 2025-12-06 · via 博客园 - daviyoung
import os
from docx import Document
from docx.shared import Cm
from docx.enum.text import WD_ALIGN_PARAGRAPH

def insert_images_simple(folder_path, output_path="图片汇总.docx", images_per_row=2, images_per_column=2):
    """
    将图片插入Word文档
    """
    # 获取图片文件
    image_extensions = ['.jpg', '.jpeg', '.png', '.gif', '.bmp', '.tiff']
    image_files = [os.path.join(folder_path, f) for f in os.listdir(folder_path) 
                   if os.path.splitext(f)[1].lower() in image_extensions]
    
    if not image_files:
        print("未找到图片文件!")
        return
    
    print(f"找到 {len(image_files)} 张图片")
    
    # 创建文档
    doc = Document()
    
    # 设置页面边距
    section = doc.sections[0]
    section.top_margin = Cm(1)
    section.bottom_margin = Cm(1)
    section.left_margin = Cm(1)
    section.right_margin = Cm(1)
    
    # 计算每页图片数
    images_per_page = images_per_row * images_per_column
    
    # 插入图片
    for i, image_path in enumerate(image_files):
        if i % images_per_page == 0:
            if i > 0:
                doc.add_page_break()
            
            # 创建表格(默认无边框)
            table = doc.add_table(rows=images_per_column, cols=images_per_row)
            
            # 设置行高
            for row in table.rows:
                row.height = Cm(10)
        
        # 计算位置
        page_pos = i % images_per_page
        row = page_pos // images_per_row
        col = page_pos % images_per_row
        
        # 插入图片
        cell = table.rows[row].cells[col]
        paragraph = cell.paragraphs[0]
        paragraph.alignment = WD_ALIGN_PARAGRAPH.CENTER
        
        try:
            # 根据行列数调整图片大小
            if images_per_row == 2 and images_per_column == 2:
                width = Cm(8)  # 2×2布局
            elif images_per_row == 3 and images_per_column == 2:
                width = Cm(5.5)  # 3×2布局
            elif images_per_row == 3 and images_per_column == 3:
                width = Cm(5)  # 3×3布局
            else:
                width = Cm(8)  # 默认
            
            paragraph.add_run().add_picture(image_path, width=width)
            
        except Exception as e:
            print(f"跳过图片 {os.path.basename(image_path)}: {e}")
    
    # 保存
    doc.save(output_path)
    print(f"✓ 文档已保存: {os.path.abspath(output_path)}")
    print(f"✓ 共插入 {len(image_files)} 张图片")
    print(f"✓ 布局: {images_per_row}×{images_per_column}")

if __name__ == "__main__":
    folder_path = r"D:\tmp\luping\shots_clean2"
    
    # 使用示例
    insert_images_simple(
        folder_path=folder_path,
        output_path="图片汇总.docx",
        images_per_row=2,
        images_per_column=2
    )