GithubHelp home page GithubHelp logo

[Bug] Ingesting docx file: DocumentFormat.OpenXml.Packaging.OpenXmlPackageException: A malformed URI was found in the document. Please provide a OpenSettings.RelationshipErrorRewriter to handle these errors while opening a package. about kernel-memory HOT 5 CLOSED

mamift avatar mamift commented on June 8, 2024
[Bug] Ingesting docx file: DocumentFormat.OpenXml.Packaging.OpenXmlPackageException: A malformed URI was found in the document. Please provide a OpenSettings.RelationshipErrorRewriter to handle these errors while opening a package.

from kernel-memory.

Comments (5)

marcominerva avatar marcominerva commented on June 8, 2024 1

Hi @mamift. With your file, I was able to recreate the problem.

This is an issue of the OpenXml library itself (see dotnet/Open-XML-SDK#715). It seems to be resolved in version 3.x, but unfortunately ClosedXML (that is used to read Excel files), is still on version 2.x. A fix for this has been merged (ClosedXML/ClosedXML#2248), but it has not been published yet. According to the milestone page (https://github.com/ClosedXML/ClosedXML/milestone/26?closed=1), it will be available in the next 0.104 version.

from kernel-memory.

marcominerva avatar marcominerva commented on June 8, 2024

I have just tried and I get no exception with a DOCX file that contains an URI like http:\\google.com. I'm using Kernel Memory v0.35.240318.1.

If you still experience the issue, could you please attach a file that showcases the problem?

from kernel-memory.

mamift avatar mamift commented on June 8, 2024

OK did a bit of more digging. Recent versions of Word (don't know the earliest version exactly,) are autocorrecting the wrong slash with https:\\ and http:\\ protocol prefixes before saving the file. If you use a non-standard protocol prefix, such as quic:\\ or test:\\, Word won't auto correct and persist the \ in the file without allowing you to re-create the exception.

It seems the DOCX files I'm working with go back up to 20 years, and have been made using a range of different Word versions, such as Word 2007, 2010 (2011 for Mac), 2013, 2016, 2019, and 365 versions etc.
And the exact pattern that triggers the error has to have at least 3 wrong slashes with non-whitespace characters after the 3rd slash. So these will trigger the error,

quic:\\1234\456
wss:\\blah\12

Strangely the following don't trigger the error; there needs to be trailing non-whitespace chars after a 3rd \:

quic:\\1234\
wss:\\blah\
http:\\intranet\

Edit: OK actually here's an example of a docx file that triggers it on my end. The files I'm working with have links to a specific custom document management system protocol:

bad uri.docx

from kernel-memory.

dluc avatar dluc commented on June 8, 2024

The latest ClosedXML 0.102.2 added a warning in case one uses a non-supported version of OpenXML, see ClosedXML/ClosedXML#2246

Closing this issue since there's nothing we can do until ClosedXML/ClosedXML#2220 is addressed and a new nuget is released. I would suggest following up with ClosedXML project, e.g. supporting with PRs and tests where possible.

from kernel-memory.

mamift avatar mamift commented on June 8, 2024

OK well for anyone else who comes across this same issue, there's a workaround: just bulk remove all links in the document and keep the text. I wrote an extension method for it:

using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;

namespace Program.Extensions;

public static class OpenXmlExtensions
{
    /// <summary>
    /// Assuming the current <paramref name="docStream"/> is a <see cref="WordprocessingDocument"/>, bulk flatten all hyperlinks (remove the link, but keep the text).
    /// </summary>
    /// <param name="docStream"></param>
    public static void BulkRemoveHyperlinks(this Stream docStream)
    {
        using var doc = WordprocessingDocument.Open(docStream, true, new OpenSettings() {
            AutoSave = true
        });

        doc.BulkRemoveHyperlinks();
    }

    /// <summary>
    /// For the current <see cref="WordprocessingDocument"/>, remove the links on all in-document hyperlinks but keep the text inline.
    /// </summary>
    /// <param name="doc"></param>
    public static void BulkRemoveHyperlinks(this WordprocessingDocument doc)
    {
        if (doc.MainDocumentPart?.Document.Body == null) return;

        foreach (var link in doc.MainDocumentPart.Document.Body.Descendants<Hyperlink>()) {
            if (!string.IsNullOrEmpty(link.Id)) {
                var hr = doc.MainDocumentPart.HyperlinkRelationships.Single(hr => hr.Id == link.Id);
                doc.MainDocumentPart.DeleteReferenceRelationship(hr.Id);
            }

            var linkText = link.InnerText;
            var run = new Run(new Text(linkText));
            link.Parent?.ReplaceChild(run, link);
        }

        if (!doc.AutoSave) {
            doc.Save();
        }
    }
}

from kernel-memory.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.