How do I debug when Writer fails to open a document?

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Locked
_savage
Posts: 198
Joined: Sun Apr 21, 2013 12:55 am

How do I debug when Writer fails to open a document?

Post by _savage »

Hi, I’ve got a DOCX file which opens without problems in Word and also in LO when opened directly like so:

Code: Select all

soffice /path/to/test.docx 
However, when I try to open that same file in a headless LO instance using Python & UNO, it comes back empty:

Code: Select all

>>> import uno
>>> local = uno.getComponentContext()
>>> resolver = local.ServiceManager.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver", local)
>>> context = resolver.resolve(f"uno:pipe,name=soffice-pipe-f0a6bad0-9764-4d18-82cc-7ba93591bacd;urp;StarOffice.ComponentContext")
>>> desktop = context.ServiceManager.createInstanceWithContext("com.sun.star.frame.Desktop", context)
>>> # Passing the `RepairPackage` property doesn't work either!
>>> document = desktop.loadComponentFromURL("file:///path/to/test.docx", "_blank", 0, ())
>>> document  # None
>>> 
No logging, no errors, nothing. There are instructions on how to build a debug version of LO but I’d rather not dig through that. So, what can I do from here?

Thanks!
Mac 13.7 using LO 24.8.2.1, Ubuntu Linux using LO 24.8 headless.
User avatar
RoryOF
Moderator
Posts: 35055
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: How do I debug when Writer fails to open a document?

Post by RoryOF »

For testing, can you open an .odt document and also a different (even trivial) .docx document using the same code? If not, the problem is most likely in the code, not in the documents.
Apache OpenOffice 4.1.15 on Xubuntu 22.04.5 LTS
_savage
Posts: 198
Joined: Sun Apr 21, 2013 12:55 am

Re: How do I debug when Writer fails to open a document?

Post by _savage »

The code isn’t the problem: I’ve been using these same Python scripts almost untouched for years and I’m able to open pretty much any other document.

Interestingly, if I load that DOCX into a recent version of Word and save, that resulting DOCX also opens without problems. There seems to be something odd with that particular DOCX file…
Mac 13.7 using LO 24.8.2.1, Ubuntu Linux using LO 24.8 headless.
User avatar
RoryOF
Moderator
Posts: 35055
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: How do I debug when Writer fails to open a document?

Post by RoryOF »

Try unzipping the bad copy of the file and the good copy, and see if you can visually identify any obvious differences.
Apache OpenOffice 4.1.15 on Xubuntu 22.04.5 LTS
User avatar
keme
Volunteer
Posts: 3774
Joined: Wed Nov 28, 2007 10:27 am
Location: Egersund, Norway

Re: How do I debug when Writer fails to open a document?

Post by keme »

Just a thought: Is that "bad" document file really a link, pointing to a file somewhere else in the file system. Modern user environments tend to mask that distinction, so file-open goes smoothly, but lower level operations (like your code) does exactly what you instruct them to do.

This type of situation has had me fooled in a couple of occasions (backup tools saving a bunch of useless link files; actual data files lost).
_savage
Posts: 198
Joined: Sun Apr 21, 2013 12:55 am

Re: How do I debug when Writer fails to open a document?

Post by _savage »

RoryOF wrote: Thu May 01, 2025 9:56 amTry unzipping the bad copy of the file and the good copy, and see if you can visually identify any obvious differences.
Phew… Both contain the same number and types of files, though the bad copy’s XML files are generally (much) shorter. I did not check whether the XML is valid and how the content itself compares but nothing stood out to me.
keme wrote: Thu May 01, 2025 10:14 amJust a thought: Is that "bad" document file really a link, pointing to a file somewhere else in the file system.
It indeed is a link to the actual file, but that hasn’t been a problem in the past for my scripts. In production — where this failed originally — it’s not a link.
Mac 13.7 using LO 24.8.2.1, Ubuntu Linux using LO 24.8 headless.
User avatar
keme
Volunteer
Posts: 3774
Joined: Wed Nov 28, 2007 10:27 am
Location: Egersund, Norway

Re: How do I debug when Writer fails to open a document?

Post by keme »

_savage wrote: Thu May 01, 2025 10:17 am [...]
It indeed is a link to the actual file, but that hasn’t been a problem in the past for my scripts. In production — where this failed originally — it’s not a link.
Of course. My bad! I guess MacOS will use proper (file system level) links, not the "OS level" linking (link files;".lnk" extension) you find in MS Windows (which is the OS of choice for most of my user base). Disregard my thought above. ;-)
Locked