Python修改Word内容

RS 技术•随笔评论757字数 2131阅读7分6秒阅读模式

最近要批量修改Word内容,尝试用Python写一段代码,留存一下。

  1. from docx import Document
  2. import os
  3.  
  4. #定义了一个modify_header函数来修改单个文档的页眉章节。该函数接受两个参数:document_path是要修改的文档路径,new_section是新的章节内容。
  5. #函数首先打开文档并获取第一个节的页眉内容。然后,它遍历页眉的段落,并将每个段落的文本内容设置为新的章节内容。
  6. def modify_header(document_path, new_section):
  7. """
  8. 修改文档的页眉章节
  9. """
  10. # 获取第一个节的页眉
  11. section = document.sections[0]
  12. header = section.header
  13.  
  14. # 修改页眉内容
  15. for paragraph in header.paragraphs:
  16. paragraph.text = new_section
  17.  
  18. #定义了一个get_file_names函数,它接受一个路径作为参数,并返回该路径下的文件名列表
  19. def get_file_names(path):
  20. """
  21. 获取指定路径下的文件名列表
  22. """
  23. file_names = []
  24. for file in os.listdir(path):
  25. if os.path.isfile(os.path.join(path, file)):
  26. file_names.append(file)
  27. return file_names
  28.  
  29. #定义了一个delete_content函数来删除文档中指定的内容。该函数接受两个参数:document_path是要删除内容的文档路径,content_to_delete是要删除的内容
  30. def delete_content(document_path, content_to_delete):
  31. """
  32. 删除文档中指定的内容
  33. """
  34. # 遍历文档的段落
  35. for paragraph in document.paragraphs:
  36. if content_to_delete in paragraph.text:
  37. # 删除包含指定内容
  38. paragraph.text = ""
  39.  
  40. # 遍历文档的表格
  41. for table in document.tables:
  42. for row in table.rows:
  43. for cell in row.cells:
  44. if content_to_delete in cell.text:
  45. # 删除包含指定内容的单元格
  46. cell.text = ""
  47.  
  48.  
  49. #授课日期列表
  50. sk_data=[
  51. '2023-04-12',
  52. '2023-04-14',
  53. '2023-04-17',
  54. '2023-04-19',
  55. '2023-04-21',
  56. '2023-04-24',
  57. '2023-04-26',
  58. '2023-04-28',
  59. '2023-05-08',
  60. '2023-05-10',
  61. '2023-05-12',
  62. '2023-05-15',
  63. '2023-05-17',
  64. '2023-05-19',
  65. '2023-05-22',
  66. '2023-05-24',
  67. '2023-05-26',
  68. '2023-05-29',
  69. '2023-05-31',
  70. '2023-06-02',
  71. '2023-06-05',
  72. '2023-06-07',
  73. '2023-06-09',
  74. '2023-06-12',
  75. '2023-06-14',
  76. '2023-06-16',
  77. '2023-06-19',
  78. '2023-06-21',
  79. ]
  80.  
  81. # 指定路径
  82. path = 'H:\\2023.6\\'
  83.  
  84. # 获取路径下的文件名列表
  85. file_names = get_file_names(path)
  86.  
  87. # 打印文件名列表,了解目录文件
  88. for file_name in file_names:
  89. print(path + file_name)
  90.  
  91.  
  92. #正式开始修改文件内容:
  93. index=0
  94. for file_name in file_names:
  95. # 打开包含表格的Word文档
  96. doc_path = path + file_name
  97. document = Document(doc_path)
  98.  
  99. #修改页眉内容
  100. new_section="信息技术"
  101. modify_header(doc_path,new_section)
  102.  
  103. #删除指定内容
  104. content_to_delete="教学设计范例"
  105. delete_content(doc_path,content_to_delete)
  106.  
  107. # 获取第一个表格
  108. table = document.tables[0]
  109.  
  110. # 获取表格的行数和列数
  111. num_rows=len(table.rows)
  112. num_columns=len(table.columns)
  113.  
  114. # 打印行数和列数
  115. print(num_rows,num_columns)
  116.  
  117. #打印授课日期下单元格内容
  118. print(table.cell(2,6).text)
  119. # 修改授课日期表格内容
  120. row_index = 2
  121. column_index = 6
  122. new_value = "2023-4-12"
  123. table.cell(row_index, column_index).text = sk_data[index]
  124. #班级
  125. table.cell(2,5).text="高一计算机"
  126.  
  127. # 保存修改后的文档
  128. output_path = path + file_name
  129. document.save(output_path)
  130. index=index+1

继续阅读
 
RS
  • 本文由 RS 发表于 2023年6月26日 08:59:50
ChatGPT提问示范 技术•随笔

ChatGPT提问示范

我想学习[插入想要的技能]。制定一个 30 天的学习计划,帮助像我这样的初学者学习和提高这项技能。 我是一个中职院校的老师,请根据商业画布理论,帮我确定我的个人商业画布要素 我是一个中职院校的老师,请...
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定