Skip to content

GitLab

  • Menu
Projects Groups Snippets
    • Loading...
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • O openpyxl
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 242
    • Issues 242
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 3
    • Merge requests 3
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Monitor
    • Monitor
    • Incidents
  • Packages & Registries
    • Packages & Registries
    • Container Registry
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar

This instance will be upgraded to Heptapod 0.31.0 (final) on 2022-05-24 at 14:00 UTC+2 (a few minutes of down time)

  • openpyxl
  • openpyxl
  • Merge requests
  • !249

Closed
Created Apr 10, 2018 by Bitbucket Importer@bitbucket_importerOwner
  • Report abuse
Report abuse

Add support for ddeLink and oleLink ExternalLink objects

  • Overview 1
  • Commits 0
  • Changes 0

Created originally on Bitbucket by 0xdecafbad (Matthew Hall)

Changesets for this Pull Request have not been imported, because it had been already declined on Bitbucket. Marked as closed by the import user.

Hi all,

Please review this pull request which adds minimal support for ddeLink and oleLink ExternalLink objects (ddeItems and oleItems not yet written).

This allows for ddeLink and oleLink items to be added as ExternalLink objects using openpyxl as such:

#!python
import openpyxl
from openpyxl.packaging.relationship import Relationship, RelationshipList
from openpyxl.workbook.external_link import ExternalLink
from openpyxl.workbook.external_link.external import ExternalBook, OleLink, DdeLink
from openpyxl.xml.functions import tostring, fromstring, Element

# Add a workbook
wb = openpyxl.Workbook()
wb.properties.saveExternalLinkValues = True
wb.properties.updateLinks = "always"
wb.properties.refreshAllConnections = True
# Grab active worksheet
ws = wb.active

### SCRIPTLET ###
xlsscriptletcode = """<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
<Relationship 
Id="rId2" 
Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/oleObject" 
Target="scriptlet:c:\windows\scriptlet.xml"
TargetMode="External"
/>
</Relationships>"""

# Create external link
scriptletlink = ExternalLink()
scriptletlink.from_tree(fromstring(xlsscriptletcode)) 
scriptletlink.file_link = Relationship(type='oleObject', Id='rId2', Target="script:c:\windows\scriptlet.xml", TargetMode="External") 
olelink = OleLink(id='rId2', progId='Package')
scriptletlink.oleLink = olelink
ws['A1']._value = """\[1]!''''""" # This is what it looks like in the underlying xlsx
ws['A1'].data_type = 'f' # Formula
wb._external_links.append(scriptletlink)

### DDEAUTO ### 
ddeautolink = ExternalLink()
ddeautolink.file_link = Relationship(type='ddeLink', Id='rId3', Target="localhost", TargetMode="Internal") 
ddecmd = "Sheet1!R1C1:R4C4"
ddelink = DdeLink(ddeService='Excel', ddeTopic=ddecmd)
ddeautolink.ddeLink = ddelink
ws['A2']._value = """\[2]!_xlbgnm.A2"""
ws['A2'].data_type = 'f'
wb._external_links.append(ddeautolink) 

filepath = 'test.xlsx' 
wb.save(filepath)

Unpacking and inspecting the resultant xlsx should show the ole/ddeLink items defined as such:

$ cat ./xl/externalLinks/externalLink2.xml
<externalLink xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"><ddeLink ddeService="Excel" ddeTopic="Sheet1!R1C1:R4C4"/></externalLink>

$ cat ./xl/externalLinks/externalLink1.xml
<externalLink xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"><oleLink xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" progId="Package" r:id="rId2"/></externalLink>

$ cat ./xl/externalLinks/_rels/externalLink2.xml.rels
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId3" Target="localhost" TargetMode="Internal" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/ddeLink"/></Relationships>

$ cat ./xl/externalLinks/_rels/externalLink1.xml.rels
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId2" Target="script:c:\windows\scriptlet.xml" TargetMode="External" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/oleObject"/></Relationships>

As its my first time submitting to openpyxl i'm unsure what tests/documentation is necessary as there already looks to be a test case for oleLink items in openpyxl/workbook/external_link/tests/data/OLELink.xml If more tests/documentation are required some guidance would be useful thanks.

Assignee
Assign to
Reviewer
Request review from
Time tracking
Source branch: bitbucket/declined-pr-249