如何在python docx中添加路径?

时间:2019-10-09 07:28:02

标签: python

我正在尝试在此代码中添加路径,我该怎么做?我不明白。



# folder path
data_folder = Path("Desktop\biologi")

if y == ('biologi'):
    document = Document() #create blank document
    document.save(y+(x.strftime(" %Y-%m-%d"))+".docx") #save blank document, lägg in path här
    document = Document(y+(x.strftime(" %Y-%m-%d"))+".docx") #open document
    p = document.add_paragraph()
    p.add_run(str(y+(x.strftime(" %Y-%m-%d"))))#edit words
    document.save(y+(x.strftime(" %Y-%m-%d"))+".docx")#save edited document

´´´

1 个答案:

答案 0 :(得分:2)

使用os库。

import os

DESKTOP_PATH = os.path.expanduser("~\Desktop")

data_folder = os.path.join(DESKTOP_PATH, 'biologi')
print(data_folder)

# prints
# C:\Users\<Username>\Desktop\biologi