吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1295|回复: 16
收起左侧

[Python 原创] 统信UOS系统,wps表格的图片批量转wps文字的python代码

[复制链接]
cqh1000 发表于 2025-3-11 11:34
[Python] 纯文本查看 复制代码
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import os
import shutil
import zipfile
from docx import Document
from docx.shared import Inches
import re
 
def natural_sort_key(s):
    return [int(text) if text.isdigit() else text.lower() for text in re.split('([0-9]+)', s)]
#!  按文件名中的数字排序
 
width_cm = 15
width_inches = Inches(width_cm / 2.54)
#! 图片宽度默认按英寸计算,这里是将15厘米换算成英寸,15厘米是A4纸打印区的标准宽度。
 
with zipfile.ZipFile('/home/user/Desktop/a.xlsx', 'r') as z:
    z.extractall('tmp')
doc = Document()
for i in sorted(os.listdir('tmp/xl/media'),key=natural_sort_key):
    doc.add_picture('tmp/xl/media/' + i,width=width_inches)
doc.save('/home/user/Desktop/out.docx')
shutil.rmtree('tmp')
 
"""
在wils大哥的代码基础上增加了图片排序问题,宽度设置问题。
使用时,源文件放在UOS系统桌面,改名为a.xlsx,运行代码后,输出文件固定为out.xlsx
放上来相当于我个人存档
再次感谢wils大哥"""


觉得有用的兄弟,还请高抬贵手帮忙加点分
代码为统信UOS下面运行,如果在windows里头用,还需要将里面的文件地址更换一下,
依赖包的安装大家就自行咨询AI吧
微信图片_2025-03-11_110423_529.png

免费评分

参与人数 1吾爱币 +5 热心值 +1 收起 理由
苏紫方璇 + 5 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

 楼主| cqh1000 发表于 2025-3-11 15:13
本帖最后由 cqh1000 于 2025-3-11 15:14 编辑

刚才的功能我又试了下deepseek,我草,哪还需要我们自己去凑代码啊,AI给的直接就能运行,而且代码也很容易读

以下是AI给的代码:

[Python] 纯文本查看 复制代码
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import os
from openpyxl import load_workbook
from docx import Document
from docx.shared import Inches
 
def excel_images_to_word(excel_path, word_path, target_width_cm=15):
    """
    将 Excel 文件中的所有图片按顺序导入 Word 并设置宽度
     
    参数:
    - excel_path: Excel 文件路径(支持 .xlsx)
    - word_path:  输出的 Word 文件路径
    - target_width_cm: 图片目标宽度(单位:厘米)
    """
    # 创建临时文件夹保存图片
    temp_dir = "excel_temp_images"
    os.makedirs(temp_dir, exist_ok=True)
 
    # 读取 Excel 文件
    wb = load_workbook(excel_path)
     
    # 提取所有图片并按顺序保存
    img_counter = 0
    image_paths = []
     
    # 遍历所有工作表
    for sheet in wb.worksheets:
        # 遍历工作表中的所有图片对象
        for img in sheet._images:
            # 生成临时文件名
            img_filename = f"image_{img_counter}.png"
            img_path = os.path.join(temp_dir, img_filename)
             
            # 保存图片到临时目录
            with open(img_path, "wb") as f:
                f.write(img._data())
             
            image_paths.append(img_path)
            img_counter += 1
 
    # 创建 Word 文档并插入图片
    doc = Document()
    target_width = Inches(target_width_cm / 2.54# 转换为英寸
     
    for img_path in image_paths:
        doc.add_picture(img_path, width=target_width)
        doc.add_paragraph()  # 添加空行分隔图片
     
    # 保存 Word 文档
    doc.save(word_path)
     
    # 清理临时文件(可选)
    for img_path in image_paths:
        os.remove(img_path)
    os.rmdir(temp_dir)
 
if __name__ == "__main__":
    excel_file = "input.xlsx"    # 输入的 Excel 文件
    word_file = "output.docx"    # 输出的 Word 文件
     
    excel_images_to_word(excel_file, word_file, target_width_cm=15)
    print(f"图片已成功导出到:{word_file}")
 楼主| cqh1000 发表于 2025-3-11 11:46
Galaxyou 发表于 2025-3-11 11:52
dysunb 发表于 2025-3-11 12:20
没用过统信的系统,支持的软件多吗
booluo 发表于 2025-3-11 12:49
通信上的软件已经不少了
Laotu 发表于 2025-3-11 14:25
为啥我通信Python环境的import pyodbc都无法安装,有没有啥教程,楼主,指导一下
Jason19821220 发表于 2025-3-11 14:26
好像用的地方不多
dhsfb 发表于 2025-3-11 14:34
感谢楼主详细的讲解
 楼主| cqh1000 发表于 2025-3-11 15:08
Laotu 发表于 2025-3-11 14:25
为啥我通信Python环境的import pyodbc都无法安装,有没有啥教程,楼主,指导一下

换个更新源试试
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2025-5-22 20:00

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表