2

I am trying to open a word document with python in windows, but I am unfamiliar with windows.

My code is as follows.

import docx as dc
doc = dc.Document(r'C:\Users\justin.white\Desktop\01100-Allergan-UD1314-SUMMARY OF WORK.docx')

Through another post, I learned that I had to put the r in front of my string to convert it to a raw string or it would interpret the \U as an escape sequence.

The error I get is

PackageNotFoundError: Package not found at 'C:\Users\justin.white\Desktop\01100-Allergan-UD1314-SUMMARY OF WORK.docx'

I'm unsure of why it cannot find my document, 01100-Allergan-UD1314-SUMMARY OF WORK.docx. The pathway is correct as I copied it directly from the file system.

Any help is appreciated thanks.

Jstuff
  • 1,266
  • 2
  • 16
  • 27
  • 1
    I don't use windows, so I can't spot anything wrong with your path name. But backslashes and spaces in file names can be tricky. You can try `import os; os.path.exists(r'C:\path\to\file')` to confirm that python can find the file. – Håken Lid May 26 '16 at 14:31

2 Answers2

1

try this

import StringIO
from docx import Document


file = r'H:\myfolder\wordfile.docx'

with open(file) as f:
    source_stream = StringIO(f.read())
document = Document(source_stream)
source_stream.close()

http://python-docx.readthedocs.io/en/latest/user/documents.html

Also, in regards to debugging the file not found error, simplify your directory names and files names. Rename the file to 'file' instead of referring to a long path with spaces, etc.

0

If you want to open the document in Microsoft Word try using os.startfile().

In your example it would be:

os.startfile(r'C:\Users\justin.white\Desktop\01100-Allergan-UD1314-SUMMARY OF WORK.docx')

This will open the document in word on your computer.

Zishe Schnitzler
  • 151
  • 2
  • 10