Get the formula in an excel file as a string in Python

How to get the formula of Excel file as a character string in Python.

After checking, it seems that you can use a library called openpyxl for XLSX files. However, the weak point is that the result of the formula cannot be obtained.

In the case of XLS file, it can be read by xlrd, but it seems that the formula itself cannot be obtained (Reference / questions / 4690423 / get-formula-from-excel-cell-with-python-xlrd)). This time, on the contrary, only the value is available.

The following is an operation experiment. Use openpyxl to create an XSLX file with formulas and save it as "test.xlsx". Furthermore, this file is read and the formula part is acquired as a character string.

import openpyxl


# 1. Create a XLSX file #

wb = openpyxl.Workbook()
ws = wb.active

# can access a cell by:
# - cell(row = 1, column = 1)
# - cell["A1"]
for i in range(1, 11):
    # note: cells are one-based
    ws.cell(row = i, column = 1).value = i
ws["C1"].value = "=SUM(A1:A10)"

wb.save("test.xlsx")


# 2. Read and get formula 

x = openpyxl.load_workbook("test.xlsx", data_only = False)
s = x.active
print("formula at C1 is...", s["C1"].value)
# Note. openpyxl does not read the computed value of cells

Recommended Posts

Get the formula in an excel file as a string in Python
The eval () function that calculates a string as an expression in python
[Python] Get the files in a folder with Python
Insert an object inside a string in Python
I want to color a part of an Excel string in Python
Open an Excel file in Python and color the map of Japan
Parse a JSON string written to a file in Python
Get the variable name of the variable as a character string.
Get the file name in a folder using glob
Download the file in Python
[Python] I tried to get the type name as a string from the type function
Find out the apparent width of a string in python
Change the standard output destination to a file in Python
Get the MIME type in Python and determine the file format
Get the number of specific elements in a python list
[Note] Import of a file in the parent directory in Python
How to get the last (last) value in a list in Python
[Python] How to output a pandas table to an excel file
Save the binary file in Python
Create an Excel file with Python3
Create a binary file in Python
Get the desktop path in Python
Get the host name in Python
Get the query string (query string) in Django
Recursively get the Excel list in a specific folder with python and write it to Excel.
I tried to create a Python script to get the value of a cell in Microsoft Excel
Create an instance of a predefined class from a string in Python
How to make a string into an array or an array into a string in Python
Format when passing a long string as an argument of python
python: Use the variables defined in the string format as they are
How to get a string from a command line argument in python
Read a file in Python with a relative path from the program
Get the number of readers of a treatise on Mendeley in Python
Write the test in a python docstring
Quickly create an excel file with Python #python
Run the Python interpreter in a script
How to get a stacktrace in python
Read the file line by line in Python
Get a token for conoha in python
In the python metaclass, pass the comparison operator as a string to the ORM method to assemble the where clause.
[GPS] Create a kml file in Python
Generate a class from a string in Python
[Python] Get the character code of the file
Get the EDINET code list in Python
[Python] Read the specified line in the file
Get a capture of the entire web page in Selenium Python VBA
A memo organized by renaming the file names in the folder with python
Get the longest character length contained in an interval in a segment tree
[Python] [Excel] Operate an Excel sheet from Python using openpyxl (using a test sheet as an example)
Test & Debug Tips: Create a file of the specified size in Python
Get a datetime instance at any time of the day in Python
I made a program to check the size of a file in Python
I get an error when I put a Python plugin in Visual Studio Code under the pyenv environment
How to input a character string in Python and output it as it is or in the opposite direction.
Create a GIF file using Pillow in Python
Get the weather in Osaka via WebAPI (python)
How to embed a variable in a python string
Read a file containing garbled lines in Python
Create an executable file in a scripting language
How to create a JSON file in Python
Make a copy of the list in Python