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

推荐订阅源

U
Unit 42
K
Kaspersky official blog
L
LangChain Blog
有赞技术团队
有赞技术团队
V
Visual Studio Blog
T
The Blog of Author Tim Ferriss
Microsoft Azure Blog
Microsoft Azure Blog
Stack Overflow Blog
Stack Overflow Blog
美团技术团队
Vercel News
Vercel News
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
G
Google Developers Blog
宝玉的分享
宝玉的分享
月光博客
月光博客
爱范儿
爱范儿
The Register - Security
The Register - Security
S
SegmentFault 最新的问题
M
MIT News - Artificial intelligence
F
Full Disclosure
Engineering at Meta
Engineering at Meta
阮一峰的网络日志
阮一峰的网络日志
Hugging Face - Blog
Hugging Face - Blog
A
About on SuperTechFans
WordPress大学
WordPress大学
D
Docker
Y
Y Combinator Blog
N
News and Events Feed by Topic
P
Proofpoint News Feed
Apple Machine Learning Research
Apple Machine Learning Research
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Recorded Future
Recorded Future
IT之家
IT之家
C
CXSECURITY Database RSS Feed - CXSecurity.com
Security Latest
Security Latest
Spread Privacy
Spread Privacy
D
Darknet – Hacking Tools, Hacker News & Cyber Security
F
Fortinet All Blogs
AWS News Blog
AWS News Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
P
Proofpoint News Feed
C
CERT Recently Published Vulnerability Notes
D
DataBreaches.Net
Recent Announcements
Recent Announcements
博客园 - Franky
Google DeepMind News
Google DeepMind News
小众软件
小众软件
B
Blog RSS Feed
I
InfoQ
Attack and Defense Labs
Attack and Defense Labs
云风的 BLOG
云风的 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
    )