[PYTHON] I want to save a file with "Do not compress images in file" set in OpenPyXL

Introduction

When opening an xlsx file saved using OpenPyXL in the desktop version of Excel, the "Do not compress images in file" checkbox is cleared. If you open a file with the check box selected and save it, it will be turned off without any questions. You can check it from the detailed settings when you open it in Excel, but since it is automatically generated in Python, I would like to omit the manual work. let's do it.

Confirmed environment

version
Python 3.8.1
openpyxl 3.0.5

How to deal with it for the time being

You can create an xlsx file with no compression settings by writing as follows.

create_xlsx.py


#!/usr/bin/env python

import openpyxl

from openpyxl.workbook.properties import WorkbookProperties
from openpyxl.utils.datetime import CALENDAR_MAC_1904
from openpyxl.workbook._writer import WorkbookWriter

def write_properties_custom(self):
    props = WorkbookProperties()
    if self.wb.code_name is not None:
        props.codeName = self.wb.code_name
    if self.wb.excel_base_date == CALENDAR_MAC_1904:
        props.date1904 = True
    props.autoCompressPictures = False
    self.package.workbookPr = props

WorkbookWriter.write_properties = write_properties_custom


wb = openpyxl.Workbook()
wb.save("Sample.xlsx")

The important thing is where props.autoCompressPictures = False. If you look at the detailed settings of the xlsx file created with this code, you can see that it is checked properly. Sample_xlsx.png

I'm forcibly rewriting the library, so it may behave unexpectedly. Please use it with caution, just in case.

Survey result memorandum

Below, I will write the contents and details of the investigation as sloppy. You don't have to read it.

In the first place

OpnePyXL has a module called Workbook Properties (https://openpyxl.readthedocs.io/en/stable/api/openpyxl.workbook.properties.html#openpyxl.workbook.properties.WorkbookProperties). There was a variable called autoCompressPictures here, so I thought it would be reflected if I set True/False to this and save it. However, there is no setting in the properties of the workbook object. However, it was present in the previous module, so I thought it wasn't unconsidered and took a look.

About save operation of OpenPyXL

Save uses the save method of the Workbook class,

I am calling write () of the WorkbookProperties class via the two files in. The property is set by write_properties () called from this write (). This time, we are adding processing to this method. I think it's okay to set it to the initial value of WorkbookProperties ().

About xlsx files

More information on workbook properties can be found here (https://docs.microsoft.com/ja-jp/dotnet/api/documentformat.openxml.spreadsheet.workbookproperties?view=openxml-2.8.1). Since autoCompressPictures compresses when it is 1 or True (default is True), Flase is set. By the way, other items in Workbook Properties of OpenPyXL are also described in detail.

About overwriting the library

I referred to this article. Overwrite library processing in Python (https://qiita.com/Asayu123/items/8d8da9911dd0c3296a81)

However, as the original form, I think that it should be set after confirmation with self.wb. *** like the two types of items described. This time it was troublesome, so I forced it to True.

Recommended Posts

I want to save a file with "Do not compress images in file" set in OpenPyXL
I want to write to a file with Python
I want to transition with a button in flask
I want to randomly sample a file in Python
I want to work with a robot in python.
I want to do ○○ with Pandas
I want to print in a comprehension
(Matplotlib) I want to draw a graph with a size specified in pixels
I want to do a monkey patch only partially safely in Python
I want to do Dunnett's test in Python
I want to create a window in Python
I want to display multiple images with matplotlib.
I want to make a game with Python
Save the object to a file with pickle
The file edited with vim was readonly but I want to save it
I want to display only different lines of a text file with diff
I want to write an element to a file with numpy and check it.
I want to set a life cycle in the task definition of ECS
I want to easily implement a timeout in python
I want to climb a mountain with reinforcement learning
I want to write in Python! (2) Let's write a test
I want to split a character string with hiragana
I want to manually create a legend with matplotlib
[ML Ops] I want to do multi-project with Python
I want to run a quantum computer with Python
I want to do something in Python when I finish
I want to bind a local variable with lambda
I want to set up a mock server for python-flask in seconds using swagger-codegen.
I want to do something like sort uniq in Python
I want to make a blog editor with django admin
I want to start a jupyter environment with one command
[Python] I want to get a common set between numpy
I want to make a click macro with pyautogui (desire)
I want to make a click macro with pyautogui (outlook)
I want to use a virtual environment with jupyter notebook!
I want to install a package from requirements.txt with poetry
[Visualization] I want to draw a beautiful graph with Plotly
I want to make input () a nice complement in python
I want to set up a GUI development environment with Python or Golang on Mac
I made a program to collect images in tweets that I liked on twitter with Python
I want to do automatic command macro file writing while using Anaconda Prompt with Atom!
I want to use a wildcard that I want to shell with Python remove
I want to solve APG4b with Python (only 4.01 and 4.04 in Chapter 4)
MacBookPro Setup After all I want to do a clean installation
I want to run Rails with rails s even in vagrant environment
I want to create a graph with wavy lines omitted in the middle with matplotlib (I want to manipulate the impression)
[Introduction] I want to make a Mastodon Bot with Python! 【Beginners】
I want to create a pipfile and reflect it in docker
I want to scrape images to learn
When generating a large number of graphs with matplotlib, I do not want to display the graph on the screen (jupyter environment)
Save a YAML-formatted file in PyYAML
I want to debug with Python
I want to specify a file that is not a character string for logrotate, but is it impossible?
I want to load the pytest fixture as a library somewhere else (pytest may not exist in the environment)
I want to solve the problem of memory leak when outputting a large number of images with Matplotlib
I want to improve efficiency with Python even in an experimental system (3) I want to do something like Excel with Pandas
Where do I stop when I set a breakpoint in a function in GDB (x86)
I want to do it with Python lambda Django, but I will stop
[Small story] How to save matplotlib graphs in a batch with Jupyter
Note: I want to do home automation with Home Assistant + Raspberry Pi + sensor # 1
What to do if you get lost in file reference with FileNotFoundError