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

相关推荐

  1. 如何word

    2024-01-29 00:56:01       52 阅读
  2. 用python 產生 PDF

    2024-01-29 00:56:01       64 阅读
  3. 重啟Debian

    2024-01-29 00:56:01       30 阅读
  4. 怎样pptx课件转换成word文档

    2024-01-29 00:56:01       30 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-29 00:56:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-29 00:56:01       101 阅读
  3. 在Django里面运行非项目文件

    2024-01-29 00:56:01       82 阅读
  4. Python语言-面向对象

    2024-01-29 00:56:01       91 阅读

热门阅读

  1. 机器学习面试题总结60-99

    2024-01-29 00:56:01       54 阅读
  2. 我来啦,我又来啦

    2024-01-29 00:56:01       43 阅读
  3. 按键控制LED灯

    2024-01-29 00:56:01       46 阅读
  4. HTML — 框架 iframe

    2024-01-29 00:56:01       59 阅读
  5. 事件的学习

    2024-01-29 00:56:01       44 阅读
  6. lc31 下一个排列

    2024-01-29 00:56:01       51 阅读
  7. 动态规划——编辑距离问题

    2024-01-29 00:56:01       54 阅读
  8. STL标准库(五) 算法,伪函数与空间适配器

    2024-01-29 00:56:01       50 阅读