我尝试复制Word文档中的所有内容,但不断丢失表格中的内容

问题描述 投票:0回答:1

我想将示例文件多次复制到output.docx。 我还在复制一次的步骤中。它粘贴了字体、格式和表格,但表格中的内容消失了。 我也是用 Google Colab 写的

!pip install python-docx
from google.colab import files
from docx import Document

# upload SAMPLE.docx file

uploaded = files.upload()
from docx import Document
from copy import deepcopy


# open SAMPLE.docx file
sample_doc = Document('SAMPLE.docx')

# copy SAMPLE.docx to output.docx

output_doc = deepcopy(sample_doc)
output_doc.save('output.docx')
print("generated output.docx")

#read output.docx

output_doc = Document('output.docx')

# adjust font size

for table in output_doc.tables:
  for row in table.rows:
    for cell in row.cells:
      for paragraph in cell.paragraphs:
        for run in paragraph.runs:
          run.font.size = 1200

# save output.docx

output_doc.save('output.docx')

我的最终计划如下

  1. 上传 SAMPLE.DOCX 和收据.XLSX

  2. 生成TEMP.DOCX

  3. 循环(与收据中的行数一样多)

    3-1.将示例复制到 TEMP

    3-2.将 TEMP 中的一些单词替换为 Receipt 中的数据(在示例中,一些单词被命名为 'client_name, Price... 等)

    3-3.将 TEMP 复制到输出

    3-4。清除温度

  4. 保存输出并下载

python duplicates copy google-colaboratory docx
1个回答
0
投票

尝试下面的代码片段:

from docx import Document

def copy_word_content(source_path, destination_path):
    # Open the source document
    source_doc = Document(source_path)

    
    destination_doc = Document() # Create or open the destination document

    
    for element in source_doc.element.body:
        destination_doc.element.body.append(element) # Copy content from source to destination

    
    destination_doc.save(destination_path) # Save the destination document

# Example usage
source_document_path = "path/to/source/document.docx"
destination_document_path = "path/to/destination/document.docx"

copy_word_content(source_document_path, destination_document_path)
© www.soinside.com 2019 - 2024. All rights reserved.