如何把word檔案自動化

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

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

import office

from docx import Document

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

doc=Document("template.docx")

table=doc.tables0

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")

相关推荐
量化炼金 (CodeAlchemy)几秒前
【量化研究】美股历史数据获取:Yahoo Finance 转 Zorro 回测格式
python·量化研究·量化交易
曹牧3 分钟前
C#:The operation has timed out
开发语言·c#
你看我名字13 分钟前
数眼智能 Search + Reader 实战:联网问答怎样避免“搜到了,却引用错了”
android·java·开发语言
小嘉丶学长15 分钟前
Day13 · 列表与元组
linux·运维·服务器·开发语言·python
努力努力再努力wz19 分钟前
【Lua入门系列】从 C++ 迭代器到 Lua 迭代器:一文深入理解 Lua 有状态以及无状态迭代器与泛型for、闭包、ipairs、pairs 和 next
开发语言·c++·lua
小小张说故事25 分钟前
Python 的 int 为什么不会溢出?拆开 PyLongObject 和它背后那串变长数组
python
努力实干27 分钟前
第二阶段 · 第 7 课:C++ 多线程与线程安全
开发语言·c++·安全
Monte053943 分钟前
word 如何让mathtype公式居中,公式编号右对齐
word
爱聊灵光的维C1 小时前
C语言初阶:关于函数设计的详细讲解和注意事项
c语言·开发语言·青少年编程·蓝桥杯·学习方法
熊猫钓鱼>_>1 小时前
上架审核常见驳回与隐私合规清单:过审前先自查这 8 条【鸿蒙心迹】
开发语言·前端·华为·华为云·harmonyos·openharmony·ai制作