[Python] The value written in Openpyxl is displayed in exponential notation (E).

Problems that occurred

The value written in the Python library openpyxl is displayed in exponential notation using E.

sample_code.py


import openpyxl

wb = openpyxl.open('./Book1.xlsx')
sh = wb['Sheet1']

sh.cell(row=1, column=1).value = 308985489249593041

wb.save('./result.xlsx')

result image.png Even if the display with the exponential function removed, some of the endings are replaced with 0s, which is a problem

Cause

Excel spec issue instead of openpyxl

If you enter a number greater than 15 digits in an Excel cell, all numbers greater than 15 digits will be changed to 0.

[Entering a long number in an Excel cell changes the last number to 0](https://docs.microsoft.com/en-us/office/troubleshoot/excel/last-digits-changed-to- from zeros)

For numerical calculations, it does not seem to be a problem if the accuracy is slightly reduced with this number of digits. It's a problem if you're writing something like an id, a credit card number, or even a single digit.

solution

Just write a number as a character string

sample_code.py


import openpyxl

wb = openpyxl.open('./Book1.xlsx')
sh = wb['Sheet1']

#I will make the numerical value to be written here str type
sh.cell(row=1, column=1).value = str(308985489249593041)

wb.save('./result.xlsx')

image.png I got angry because this is a string or a number that starts with an apostrophe, but I could write it By the way, the calculation in Excel can be done normally even if this error occurs.

How can I get the value written as a character string in openpyxl?

sample_code.py


import openpyxl

wb = openpyxl.open('./result.xlsx')
sh = wb['Sheet1']

A1_value = sh.cell(row=1, column=1).value
print(A1_value)
print(type(A1_value))

'''
> 308985489249593041
> <class 'str'>

It seems to be obtained as a character string I have to replace it with int again

Postscript: I will specify the format

The following articles were introduced in the comments. Specify the display format of numbers with openpyxl

That's why I passed some formats to sh.cell (row =, colum =). number_format =. The number to write is the same as above 308985489249593041 image.png In the format given by column A, column B is the result Oh, it's amazing. The format has changed properly. But unfortunately the end can be changed to 0 ... It's a B2 or character string format, but it seems impossible if it's an int type at the time of writing ...

Afterword

Is there a smarter workaround? You can release the upper limit with an Excel plug-in w If you know, please let me know

Recommended Posts

[Python] The value written in Openpyxl is displayed in exponential notation (E).
What to do when the value type is ambiguous in Python?
Where is the python instantiation process written?
What is "mahjong" in the Python library? ??
The date is displayed incorrectly in matplotlib.
What is wheezy in the Docker Python image?
About the difference between "==" and "is" in python
Find the divisor of the value entered in python
Convert exponential notation float to str in Python
Even if the development language is changed to python3 in Cloud9, version 2 is displayed in python --version
Check if the string is a number in python
[python] Method to darken RGB value in hexadecimal notation
Carefully understand the exponential distribution and draw in Python
What is the domain attribute written in Plotly's Layout?
Find the part that is 575 from Wikipedia in Python
Get the value selected in Selenium Python VBA pull-down
What to do if the progress bar is not displayed in tqdm of python
Class notation in Python
I want to initialize if the value is empty (python)
Test.py is not reflected on the web server in Python3.
Comparison of exponential moving average (EMA) code written in Python
Get the value while specifying the default value from dict in Python
Grayscale image is displayed as a color image in OpenCV / Python
How to get the last (last) value in a list in Python
Automatically get the port where Arduino is stuck in Python
When "No changes detected" is displayed in python3 manage.py makemigrations
Download the file in Python
Find the difference in Python
Shapley value calculation in Python
Solve ABC176 E in Python
Convert CIDR notation in Python
Gacha written in Python -BOX gacha-
In the python dictionary, if a non-existent key is accessed, initialize it with an arbitrary value
A simple reason why the return value of round (2.675,2) is 2.67 in python (it should be 2.68 in reality ...)
How is the progress? Let's get on with the boom ?? in Python
Play a sound in Python assuming that the keyboard is a piano keyboard
Be careful when specifying the default argument value in Python3 series
[Python] Execution time when a function is entered in a dictionary value
Delete a particular character in Python if it is the last
How to use the asterisk (*) in Python. Maybe this is all? ..
Solve the smallest value in Python (equivalent to paiza rank D)
Python script written in PyTorch is converted to exe with PyInstaller
Squid Lisp written in Python: Hy
Getting the arXiv API in Python
[Python] Find the second smallest value.
Included notation in Python function arguments
Use fabric as is in python (fabric3)
Python in the browser: Brython's recommendation
Hit the Sesami API in Python
Python is UnicodeEncodeError in CodeBox docker
Get the desktop path in Python
[Python] What is @? (About the decorator)
Get the script path in Python
In the python command python points to python3.8
Implement the Singleton pattern in Python
Compatibility diagnosis program written in python
[python] What is the sorted key?
Switch the language displayed in Django 1.9
Hit the web API in Python
There is no switch in python
I wrote the queue in Python