Python修改Word內容

  • 0 views
  • 閱讀模式

最近要批量修改Word內容,嘗試用Python寫一段代碼,留存一下。

from docx import Document
import os

#定義了一個modify_header函數來修改單個文檔的頁眉章節。該函數接受兩個參數:document_path是要修改的文檔路徑,new_section是新的章節內容。
#函數首先打開文檔并獲取第一個節的頁眉內容。然后,它遍歷頁眉的段落,并將每個段落的文本內容設置為新的章節內容。
def modify_header(document_path, new_section):
    """
    修改文檔的頁眉章節
    """
    # 獲取第一個節的頁眉
    section = document.sections[0]
    header = section.header

    # 修改頁眉內容
    for paragraph in header.paragraphs:
        paragraph.text = new_section

#定義了一個get_file_names函數,它接受一個路徑作為參數,并返回該路徑下的文件名列表
def get_file_names(path):
    """
    獲取指定路徑下的文件名列表
    """
    file_names = []
    for file in os.listdir(path):
        if os.path.isfile(os.path.join(path, file)):
            file_names.append(file)
    return file_names

#定義了一個delete_content函數來刪除文檔中指定的內容。該函數接受兩個參數:document_path是要刪除內容的文檔路徑,content_to_delete是要刪除的內容
def delete_content(document_path, content_to_delete):
    """
    刪除文檔中指定的內容
    """
    
    # 遍歷文檔的段落
    for paragraph in document.paragraphs:
        if content_to_delete in paragraph.text:
            # 刪除包含指定內容
            paragraph.text = ""

    # 遍歷文檔的表格
    for table in document.tables:
        for row in table.rows:
            for cell in row.cells:
                if content_to_delete in cell.text:
                    # 刪除包含指定內容的單元格
                    cell.text = ""


#授課日期列表        
sk_data=[
'2023-04-12',
'2023-04-14',
'2023-04-17',
'2023-04-19',
'2023-04-21',
'2023-04-24',
'2023-04-26',
'2023-04-28',
'2023-05-08',
'2023-05-10',
'2023-05-12',
'2023-05-15',
'2023-05-17',
'2023-05-19',
'2023-05-22',
'2023-05-24',
'2023-05-26',
'2023-05-29',
'2023-05-31',
'2023-06-02',
'2023-06-05',
'2023-06-07',
'2023-06-09',
'2023-06-12',
'2023-06-14',
'2023-06-16',
'2023-06-19',
'2023-06-21',
]

# 指定路徑
path = 'H:\\2023.6\\'

# 獲取路徑下的文件名列表
file_names = get_file_names(path)

# 打印文件名列表,了解目錄文件
for file_name in file_names:
    print(path + file_name)


#正式開始修改文件內容:
index=0
for file_name in file_names:
    # 打開包含表格的Word文檔
    doc_path = path + file_name
    document = Document(doc_path)

    #修改頁眉內容
    new_section="信息技術"
    modify_header(doc_path,new_section)

    #刪除指定內容
    content_to_delete="教學設計范例"
    delete_content(doc_path,content_to_delete)

    # 獲取第一個表格
    table = document.tables[0]

    # 獲取表格的行數和列數
    num_rows=len(table.rows)
    num_columns=len(table.columns)

    # 打印行數和列數
    print(num_rows,num_columns)

    #打印授課日期下單元格內容
    print(table.cell(2,6).text)
    # 修改授課日期表格內容
    row_index = 2
    column_index = 6
    new_value = "2023-4-12"
    table.cell(row_index, column_index).text = sk_data[index]
    #班級
    table.cell(2,5).text="高一計算機"

    # 保存修改后的文檔
    output_path = path + file_name
    document.save(output_path)
    index=index+1

繼續閱讀
RS
  • 本文由 發表于 2023年6月26日 08:59:50
pve 7.2升級7.4 技術?隨筆

pve 7.2升級7.4

源的修改: 一、刪除企業源:nano /etc/apt/sources.list.d/pve-enterprise.list 注釋掉這句: <p>##deb https://enterpr...
評論  1  訪客  1
    • 倚天私服網
      倚天私服網 2

      倚天私服網 倚天搜服網 http://www.ytsf.com
      倚天私服網,玩家最信賴的倚天找服網站。多站同步,更好找服、找家族、找玩伴。倚天私服倚天搜服倚天1開區信息、玩家信息、家族信息等。網址:www.ytsf.com

    匿名

    發表評論

    匿名網友 填寫信息

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

    確定