Python 为docx模块提供正确的路径

Python 为docx模块提供正确的路径,python,path,docx,Python,Path,Docx,我写了一个需要.docx文件才能工作的代码。为了避免“docx.opc.exceptions.PackageNotFoundError:Package not found at”错误,我必须按如下方式编写路径: from docx import Document from docx.shared import Pt from docx.enum.text import WD_ALIGN_PARAGRAPH document = Document("C:/Users/Murat/

我写了一个需要.docx文件才能工作的代码。为了避免“docx.opc.exceptions.PackageNotFoundError:Package not found at”错误,我必须按如下方式编写路径:

from docx import Document
from docx.shared import Pt
from docx.enum.text import WD_ALIGN_PARAGRAPH    
document = Document("C:/Users/Murat/Desktop/Combobox/dist/Data.docx/Data.docx")
style = document.styles['Normal']
font = style.font
font.name = 'Times New Roman'
font.size = Pt(12)
但这意味着只有我可以使用这个程序!或者使用相同用户名的。 我使用“~”更改了路径,但无效:

document = Document("~/Desktop/Combobox/dist/Data.docx/Data.docx")
你有什么建议?

几种可能性:

  • 使用py文件中的相对路径
  • 使用
    \uuuu file\uuuu
    获取python路径,使用os.path.join获取文档的绝对路径
  • 使用对话框选择文档:

  • 如何使用相对路径?我是Python的新手。我也不知道这个文件。就像在任何语言中:
    “\data.docx'
    如果文档在父文件夹中或使用尽可能多的
    。\
    as needs您的意思是:document=document(“../../Desktop/Combobox/dist/Data.docx/Data.docx”),一个用于“C:/Users”,另一个用于用户名。