python-docx 如何将docx保存到特定路径?

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

我使用 python-docx 中的示例,在我 运行 代码之后我找不到 docx 文件在哪里我可以指出我想要添加的特定路径吗?

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)
document.save('C:\Users\Administrator\Desktop\python test\update_test\' + 'test.docx')

这里没有解释的是 Python 不读取带有单个反斜杠的 filepath。通常,它读作双反斜杠或单正斜杠。 如果您这样做,您原来的方法确实有效:

filePath = 'test2.docx'
doc.save('J:/drive/testFolder/CloudExile/' + filePath)

doc.save('J:\drive\testFolder\CloudExile\' + filePath)

我希望这有助于让任何看到这个的人有更多的见解。