Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
.vscode
__pycache__
process_history.csv
**/.venv
28 changes: 14 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Compress Office
# Compress Office

English | [中文](README_ZH_CN.md)

Expand All @@ -10,22 +10,22 @@ A simple tool that use [ImageOptim](https://github.com/ImageOptim/ImageOptim) to

Clone this repo first:

```
```bash
git clone https://github.com/cometeme/compress-office.git
```

Then we need:

1. ImageOptim: https://github.com/ImageOptim/ImageOptim
2. Python 3.8 (and you need to install `rich` package via `pip`)
3. trash: https://github.com/ali-rantakari/trash
4. fd (Optional, can optimize searching speed): https://github.com/sharkdp/fd
1. ImageOptim: <https://github.com/ImageOptim/ImageOptim>
2. Python >= 3.8 (and you need to install `rich` package via `pip`)
3. fd (Optional, can optimize searching speed): <https://github.com/sharkdp/fd>

If you have Homebrewrun these commands to install dependencies (or install manually):
If you have Homebrew, run these commands to install dependencies (or install manually):

```
brew install imageoptim python@3.8 trash
pip3.8 install rich
```bash
brew install imageoptim python@3
python3 -m venv .venv && source .venv/source/active
pip install rich
```

Then start ImageOptim, open "Preferences" menu and modify the settings. You can choose to perform lossless or lossy compression on the pictures in the document. The former can maintain the image quality, while the latter can better reduce size. You can also choose whether to remove EXIF information. EXIF contains informations about the time, gps and camera settings. Removes EXIF in the picture can better protect your privacy.
Expand All @@ -34,14 +34,14 @@ Then start ImageOptim, open "Preferences" menu and modify the settings. You can

Run `compress-office.py` with files or folders you want to compress. You can pass in multiple paths. If you pass in a directory, the program will traverse in the directory and find files that can be compressed.

```
python3.8 compress-office.py [path1] [path2] ...
```bash
python compress-office.py [path1] [path2] ...
```

For example:

```
python3.8 compress-office.py ~/Documents ./test.docx
```bash
python compress-office.py ~/Documents ./test.docx
```

When program run for the first time, it will create a file called `process_history.csv`, which records the path of the compressed file and its modify time. When running again, if program finds that the file has not changed (file path is in the history and its modify time is same as recorded), then the program will skip the file without re-compressing, because doing so is not meaningful. If you really need to recompress a file, just delete its record from csv file.
Expand Down
32 changes: 16 additions & 16 deletions README_ZH_CN.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Compress Office
# Compress Office

[English](README.md) | 中文

Expand All @@ -10,38 +10,38 @@

首先下载本项目:

```
```bash
git clone https://github.com/cometeme/compress-office.git
```

接下来需要安装以下环境:

1. ImageOptim: https://github.com/ImageOptim/ImageOptim
2. Python 3.8 (并且需要使用 `pip` 安装 `rich` 包)
3. trash: https://github.com/ali-rantakari/trash
4. fd (可选,配置本项可以加快查找文件的速度): https://github.com/sharkdp/fd
1. ImageOptim: <https://github.com/ImageOptim/ImageOptim>
2. Python >= 3.8 (并且需要使用 `pip` 安装 `rich` 包)
3. fd (可选,配置本项可以加快查找文件的速度): <https://github.com/sharkdp/fd>

如果你使用 Homebrew ,运行以下指令就能完成环境配置:
如果你使用 Homebrew,运行以下指令就能完成环境配置:

```
brew install imageoptim python@3.8 trash
pip3.8 install rich
```bash
brew install imageoptim python@3
python3 -m venv .venv && source .venv/source/active
pip install rich
```

接下来运行 ImageOptim ,打开“偏好设置”,根据你的需求配置一下 ImageOptim 的参数。通过设置 ImageOptim 的参数,可以选择对文档中的图片进行无损压缩或是有损压缩,前者能够保持图像质量,而后者能够更大程度减小体积。同时还可以选择是否去除 EXIF 信息,EXIF 中包含了拍摄时间、地点、设备等信息,去除图片的 EXIF 能够更好地防止隐私泄露。
接下来运行 ImageOptim,打开“偏好设置”,根据你的需求配置一下 ImageOptim 的参数。通过设置 ImageOptim 的参数,可以选择对文档中的图片进行无损压缩或是有损压缩,前者能够保持图像质量,而后者能够更大程度减小体积。同时还可以选择是否去除 EXIF 信息,EXIF 中包含了拍摄时间、地点、设备等信息,去除图片的 EXIF 能够更好地防止隐私泄露。

## 使用教程

运行 `compress-office.py` ,传入需要压缩的文件或目录即可开始压缩。可以传入多个路径,如果传入的是目录,那么程序会遍历目录中的所有文件,并找到可以压缩的文档。

```
python3.8 compress-office.py [路径1] [路径2] ...
```bash
python compress-office.py [路径1] [路径2] ...
```

例如:

```
python3.8 compress-office.py ~/Documents ./test.docx
```bash
python compress-office.py ~/Documents ./test.docx
```

第一次运行时,程序会创建一个叫 `process_history.csv` 的文件,其中记录了已经被压缩的文件的路径以及其修改时间。当再次运行时,如果程序发现文件没有改变(这个文件在历史记录中,并且它的修改时间与记录的相同),那么它会直接跳过这个文件,而不会重新进行压缩,因为重新压缩是没有意义的。如果你真的需要重新压缩某个文件,从 csv 文件中删除对应文件的记录即可。
Expand All @@ -64,7 +64,7 @@ A1: 可以,不过首先你需要找到一个 ImageOptim 的替代品,例如

A2: docx/pptx/xlsx 文档本质上就是一个 zip 压缩包,其中的资源都被打包在一起。本程序会将用户输入的文档逐个解压至一个缓存目录中,调用 ImageOptim 压缩缓存目录中的所有图片,再将其重新压缩,放回原处。

因此,用本程序压缩文档**理论上**不会造成文档损坏,但是为了预防不可预知的 bug ,建议您在使用前对文档进行备份。同时,本程序会在压缩后将原始文档移至回收站中,如果发现问题可以从回收站还原原文件。
因此,用本程序压缩文档**理论上**不会造成文档损坏,但是为了预防不可预知的 bug,建议您在使用前对文档进行备份。同时,本程序会在压缩后将原始文档移至回收站中,如果发现问题可以从回收站还原原文件。

### Q3: 为什么压缩之后,回收站会出现很多图片?

Expand Down
7 changes: 5 additions & 2 deletions compress-office.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
from glob import glob
from os.path import abspath, exists, isfile, join
from shlex import quote
Expand Down Expand Up @@ -29,8 +30,10 @@
arg = abspath(arg)

if isfile(arg):
if not arg.split(".")[-1] in exts:
console.print(f"Error: {arg} is not a supported file.", style="bold red")
if arg.split(".")[-1] not in exts:
console.print(
f"Error: {arg} is not a supported file.", style="bold red"
)
exit(-1)
if not his.file_in_history(arg):
file_paths.append(arg)
Expand Down
16 changes: 12 additions & 4 deletions function.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
from os import mkdir
from os.path import abspath, basename, exists, expanduser, getsize, join
from shutil import move, rmtree
Expand Down Expand Up @@ -63,15 +64,22 @@ def compress(file_path: str) -> Tuple:
return (before_size, before_size)

# Move new file to origin place
run(
["trash", file_path],
).check_returncode()
# run(
# ["trash", file_path],
# ).check_returncode()
file_extension = file_path.split(".")[-1] # docx
move(
file_path,
file_path.replace("." + file_extension, "_uncompressed." + file_extension),
)
move(new_file_path, file_path)

# Delete caches
if exists(cache_folder):
rmtree(cache_folder)

print(f"Successfully compressed {convert_size(before_size)} -> {convert_size(after_size)}")
print(
f"Successfully compressed {convert_size(before_size)} -> {convert_size(after_size)}"
)

return (before_size, after_size)
4 changes: 3 additions & 1 deletion history.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# -*- coding: utf-8 -*-
import csv
from os.path import abspath, exists
import os
from os.path import abspath, exists


class history:
def __init__(self, path: str) -> None:
Expand Down