Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python docx如何将docx保存到特定路径?_Python_Python Docx - Fatal编程技术网

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

python docx如何将docx保存到特定路径?,python,python-docx,Python,Python Docx,我使用PythonDocx中的示例,在运行代码后,我找不到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 =

我使用PythonDocx中的示例,在运行代码后,我找不到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)

这里没有解释的是Python不会读取带有单个反斜杠的
文件路径。通常,它读作双反斜杠或单正斜杠。
如果执行以下操作,则原始方法确实有效:

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


我希望这有助于让任何人了解这一点。

更新测试是一个文件夹吗?如果需要,请在
filepath
varibale中的后面再加一个“\”。另外,使用
os
模块来完成这项工作可能更有益。谢谢回复,是的update\u test是一个文件夹名,我尝试添加“\”但仍然不起作用。我建议您使用
filepath=os.path.join(r'C:\Users\Administrator\Desktop\python test\update\u test',filename)
而不是字符串连接,它处理自动添加反斜杠。你确定桌面上的图标不只是隐藏在某个地方吗?建议您打开一个桌面资源管理器窗口并按日期排序。谢谢Martin,现在这对我有用。回答得好,通常是一个好主意,可以添加一个解释。欢迎使用堆栈溢出!请正确格式化您的代码。
filePath = 'test2.docx'
doc.save('J:/drive/testFolder/CloudExile/' + filePath)
doc.save('J:\\drive\\testFolder\\CloudExile\\' + filePath)