如何把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")

相关推荐
叫我辉哥e134 分钟前
### 技术文章大纲:C语言造轮子大赛
c语言·开发语言
Hgfdsaqwr2 小时前
Django全栈开发入门:构建一个博客系统
jvm·数据库·python
guygg882 小时前
NOMA功率分配与64 QAM调制中的SIC的MATLAB仿真
开发语言·matlab
开发者小天2 小时前
python中For Loop的用法
java·服务器·python
flushmeteor2 小时前
JDK源码-基础类-String
java·开发语言
老百姓懂点AI2 小时前
[RAG实战] 向量数据库选型与优化:智能体来了(西南总部)AI agent指挥官的长短期记忆架构设计
python
u0109272713 小时前
C++中的策略模式变体
开发语言·c++·算法
雨季6663 小时前
构建 OpenHarmony 简易文字行数统计器:用字符串分割实现纯文本结构感知
开发语言·前端·javascript·flutter·ui·dart
雨季6663 小时前
Flutter 三端应用实战:OpenHarmony 简易倒序文本查看器开发指南
开发语言·javascript·flutter·ui
进击的小头4 小时前
行为型模式:策略模式的C语言实战指南
c语言·开发语言·策略模式