0

I wrote a code which needs a .docx file to work. To avoid "docx.opc.exceptions.PackageNotFoundError: Package not found at" error I had to write the path as follows:

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)

But this means that only I can use the program! Or the ones with the same username. I changed the path using "~" but it didn't work:

document = Document("~/Desktop/Combobox/dist/Data.docx/Data.docx")

What do you suggest?

mdiramali
  • 1
  • 2

1 Answers1

0

Several possibilities:

  1. use relative path from your py file
  2. use __file__ to get the python path and os.path.join to get absolute path of document
  3. use a dialog to select the document : Quick and easy file dialog in Python?
DonKnacki
  • 226
  • 1
  • 10
  • How can I use a relative path? I am newbie in Python. I didn't know __file__ either. – mdiramali May 23 '21 at 17:27
  • Like in any language: `'.. \data.docx'` if document is in parent folder or use as many `.. \ ` as need – DonKnacki May 23 '21 at 17:28
  • Do you mean this: document = Document("../../Desktop/Combobox/dist/Data.docx/Data.docx") One for "C:/Users" and one for the username. – mdiramali May 23 '21 at 17:37