[Python] python-docx how can i save docx to the specific path?

Discussão em 'Python' iniciado por Stack, Maio 10, 2025.

  1. Stack

    Stack Membro Participativo

    I use the sample from python-docx and after I run the code I cannot find where is the docx file can I point specific path I want to add?

    from docx import Document
    from docx.shared import Inches

    document = Document('C:\Users\Administrator\Desktop\python test\update_test\\test.docx')

    document.add_heading('Document Title', 0)

    p = document.add_paragraph('A plain paragraph having some ')
    p.add_run('bold').bold = True
    p.add_run(' and some ')
    p.add_run('italic.').italic = True

    document.add_heading('Heading, level 1', level=1)
    document.add_paragraph('Intense quote', style='Intense Quote')

    document.add_paragraph(
    'first item in unordered list', style='List Bullet'
    )
    document.add_paragraph(
    'first item in ordered list', style='List Number'
    )

    filename='test.docx'


    filepath=r'C:\Users\Administrator\Desktop\python test\update_test'+filename

    document.add_page_break()

    document.save(filepath)

    Continue reading...

Compartilhe esta Página