如何把word檔案自動化

1.安裝 pip install jojo-office ( 安裝這個)

2.讀取表格內容與文章內容,範例如下

import office

from docx import Document

replace={"{user_name}":"陳彥甫", "{tele}":"0912882881"}

doc=Document("template.docx")

table=doc.tables[0]

print("ii:"+ str(len(table.rows)))

print("jj:"+ str(len(table.columns)))

for old,new in replace.items():

print(r,w)

for table in doc.tables:

for col in table.columns:

for cell in col.cells:

if cell.text == old:

cell.text = new

print(cell.text)

复制代码
for paragraph in doc.paragraphs:
    # 整串找到有字串一樣的地方,就直接更換
    if paragraph.text.find(old) >= 0:        
        paragraph.text = paragraph.text.replace(old,new)
        print("OK")  #找到,整串換掉

doc.save("test.docx")

相关推荐
csbysj20201 天前
Bootstrap 弹出框
开发语言
重庆小透明1 天前
【面试问题】java小厂
java·开发语言·面试
smj2302_796826521 天前
解决leetcode第3869题.统计区间内奇妙数的数目
python·算法·leetcode
CodeCraft Studio1 天前
Parasoft C/C++嵌入式软件测试解决方案:安全、可靠且符合标准
开发语言·c++·安全·单元测试·代码规范·parasoft·嵌入式软件测试
AI视觉网奇1 天前
pycharm ui 历史版本
python
咚为1 天前
Rust 跨平台编译实战:从手动配置到 Cross 容器化
开发语言·后端·rust
只与明月听1 天前
RAG深入学习之Emabedding
前端·python·面试
永远睡不够的入1 天前
C++STL详解2:stack和queue
开发语言·c++
2401_883035461 天前
数据分析与科学计算
jvm·数据库·python
宵时待雨1 天前
C++笔记归纳11:多态
开发语言·c++·笔记