Identify external workbook links using openpyxl

2024/9/20 11:58:11

I am trying to identify all cells that contain external workbook references, using openpyxl in Python 3.4. But I am failing. My first try consisted of:

def find_external_value(cell):
# identifies an external link in a given cellif '.xls' in cell.value:has_external_reference = Truereturn has_external_value

However, when I print the cell values that have external values to the console, it yields this:

=[1]Sheet1!$B$4
=[2]Sheet1!$B$4

So, openpyxl obviously does not parse formulas containing external values in the way I imagined and since square brackets are used for table formulas, there is no sense in trying to pick up on external links in this manner.

I dug a little deeper and found the detect_external_links function in the openpyxl.workbook.names.external module (reference). I have no idea if one can actually call this function to do what I want.

From the console results it seems as if openpyxl understands that there are references, and seems to contain them in a list of sorts. But can one access this list? Or detect if such a list exists?

Whichever way - all I need is to figure out if a cell contains a link to an external workbook.

Answer

I have found a solution to this. Use the openpyxl library for load the xlsx file as

import openpyxl
wb=openpyxl.load_workbook("Myworkbook.xlsx")"""len(wb._external_links)        *Add this line to get count of linked workbooks*"""items=wb._external_links
for index, item in enumerate(items):Mystr =wb._external_links[index].file_link.TargetMystr=Mystr.replace("file:///","")print(Mystr.replace("%20"," "))----------------------------
Out[01]: ##Indicates that the workbook has 4 external workbook links##
/Users/myohannan/AppData/Local/Temp/49/orion/Extension Workpapers_Learning Extension Calc W_83180610.xlsx
/Users/lmmeyer/AppData/Local/Temp/orion/Complete Set of Workpapers_PPS Workpapers 123112_111698213.xlsx
\\SF-DATA-2\IBData\TEMP\ie5\Temporary Internet Files\OLK8A\LBO Models\PIGLET Current.xls
/WINNT/Temporary Internet Files/OLK3/WINDOWS/Temporary Internet Files/OLK8304/DEZ.XLS     
https://en.xdnf.cn/q/72506.html

Related Q&A

3D-Stacked 2D histograms

I have a bunch of 2D histograms (square 2D numpy arrays) that I want to stack in 3D like so:(Image from: Cardenas, Alfredo E., et al. "Unassisted transport of N-acetyl-L-tryptophanamide through me…

Python and mySQLdb error: OperationalError: (1054, Unknown column in where clause)

Hey all, Im getting an error OperationalError: (1054, "Unknown column XX in where clause")Where XX is the value of CLASS in the following codeconn = MySQLdb.connect(host = "localhost&quo…

Best Python GIS library? [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, argum…

Build a class with an attribute in one line

How do I write a one-liner for the following? class MyClass(): content = {} obj = MyClass()

Python Imports, Paths, Directories Modules

Let me start by saying Ive done extensive research over the course of the past week and have not yet found actual answers to these questions - just some fuzzy answers that dont really explain what is g…

Finding location in code for numpy RuntimeWarning

I am getting warnings like these when running numpy on reasonably large pipeline. RuntimeWarning: invalid value encountered in true_divideRuntimeWarning: invalid value encountered in greaterHow do I fi…

Django, Angular, DRF: Authentication to Django backend vs. API

Im building an app with a Django backend, Angular frontend, and a REST API using Django REST Framework for Angular to consume. When I was still working out backend stuff with a vanilla frontend, I used…

Django view testing

Im trying to figure out if there is a quick way to test my django view functions form either the python or django shell. How would I go about instantiating and passing in faux HTTPrequest object?

Remove non-ASCII characters from string columns in pandas

I have panda dataframe with multiple columns which mixed with values and unwanted characters. columnA columnB columnC ColumnD \x00A\X00B NULL \x00C\x00D 123 \x00E\X00F…

Open source Twitter clone (in Ruby/Python) [closed]

Closed. This question is seeking recommendations for books, tools, software libraries, and more. It does not meet Stack Overflow guidelines. It is not currently accepting answers.We don’t allow questi…