使用Python复制从Word File到PowerPoint Textbox的同一格式的文本

发布于 2025-02-08 22:58:49 字数 1642 浏览 1 评论 0原文

我正在尝试使用Python将文本从Word文档复制到PowerPoint文本框中。我希望与一个完全相同的格式中的Word文档中的所有文本插入一个PowerPoint文本框。这里的问题是其中一些段落是粗体的,有些则不是。我不确定这是否可能。请参阅下面的Word Doc的样子: -

word doc doc

这就是应该看起来的外观就像在ppt中(在一个文本框中)

powerpoint

我正在使用python docx和pptx库但是我不是他们的专家。请帮忙!

编辑:

到目前为止,我尝试过的一些事情: -

我找到了一个代码,可以复制带有样式的文本,但它是用于Word到Word的。

def get_para_data(output_doc_name, paragraph):
    """
    Write the run to the new file and then set its font, bold, alignment, color etc. data.
    """

    output_para = output_doc_name.add_paragraph()
    for run in paragraph.runs:
        output_run = output_para.add_run(run.text)
        # Run's bold data
        output_run.bold = run.bold
        # Run's italic data
        output_run.italic = run.italic
        # Run's underline data
        output_run.underline = run.underline
        # Run's color data
        output_run.font.color.rgb = run.font.color.rgb
        # Run's font data
        output_run.style.name = run.style.name
    # Paragraph's alignment data
    output_para.paragraph_format.alignment = paragraph.paragraph_format.alignment
input_doc = Document('InputDoc.docx')
output_doc = Document()

# Call the function
get_para_data(output_doc, input_doc.paragraphs[3])

# Save the new file
output_doc.save('OutputDoc.docx')

请让我知道如何从Word到PowerPoint。

I'm trying to copy texts from the word document into a PowerPoint text box using Python. I want all the texts in the word document in the exact same formatting into one PowerPoint text box. The problem here is some of the paragraphs are in bold and some are not. I'm not sure if this is even possible. Please see below how the word doc looks like:-

Word Doc

And this is how it should look like in the ppt (In one text box)

PowerPoint

I'm using python docx and pptx library but I'm not expert in them. Please help!

Edit:

some of the things I've tried so far:-

I found one code to copy texts with styles but it is for word to word.

def get_para_data(output_doc_name, paragraph):
    """
    Write the run to the new file and then set its font, bold, alignment, color etc. data.
    """

    output_para = output_doc_name.add_paragraph()
    for run in paragraph.runs:
        output_run = output_para.add_run(run.text)
        # Run's bold data
        output_run.bold = run.bold
        # Run's italic data
        output_run.italic = run.italic
        # Run's underline data
        output_run.underline = run.underline
        # Run's color data
        output_run.font.color.rgb = run.font.color.rgb
        # Run's font data
        output_run.style.name = run.style.name
    # Paragraph's alignment data
    output_para.paragraph_format.alignment = paragraph.paragraph_format.alignment
input_doc = Document('InputDoc.docx')
output_doc = Document()

# Call the function
get_para_data(output_doc, input_doc.paragraphs[3])

# Save the new file
output_doc.save('OutputDoc.docx')

Please let me know how to do this from word to PowerPoint.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文