Skip to main content

通过 Excel COM、SQLite 和独立子进程重建 xlsx 工作簿

Project description

x2x

通过 Excel COM 读取 .xlsx,暂存到 SQLite,再由独立 Python 进程重新构造工作簿。

快速开始

运行环境:

  • Windows
  • Python 3.10 或更高版本
  • Microsoft Excel

安装:

python -m pip install x2x-excel

转换文件:

x2x test.xlsx

安装包名是 x2x-excel,安装后的命令名是 x2xopenpyxlpywin32 会由 pip 自动安装。

默认在源文件旁生成 <源文件名>_rebuilt.xlsx。例如,以上命令会生成 test_rebuilt.xlsx

常用命令

指定输出文件:

x2x test.xlsx -o result.xlsx

一次转换多个文件:

x2x test.xlsx test1.xlsx test2.xlsx

批量模式按输入顺序处理,并分别生成 test_rebuilt.xlsxtest1_rebuilt.xlsxtest2_rebuilt.xlsx-o/--output 只适用于单文件模式。

路径包含中文或空格时使用引号:

x2x "C:\Excel 文件\test.xlsx" -o "C:\Excel 文件\result.xlsx"

查看帮助:

x2x --help

常见问题

找不到 x2x 命令

先尝试通过 Python 模块运行:

python -m x2x test.xlsx

如果该命令可用,请将当前 Python 的 Scripts 目录加入 Windows PATH

安装过 PyPI 上旧的 x2x

PyPI 已有一个与本项目无关的 x2x 包,并且可能安装同名命令。请先卸载旧包,再安装本项目:

python -m pip uninstall x2x
python -m pip install --upgrade x2x-excel

输出文件已存在

为避免误覆盖,程序会拒绝写入已有文件。请删除旧输出,或通过 -o 指定新文件名。

保留范围

当前版本会保留:

  • 工作表名称、顺序和可见性
  • 单元格空值、文本、数字、布尔值、错误值及公式
  • 单元格数字格式,例如日期格式和文本格式
  • 工作簿的 1900/1904 日期基准
  • 使用区域内的行高、列宽及隐藏状态

长编号会按文本保存,避免科学计数法或精度损失。公式会在工作簿下次由 Excel 打开时重新计算。

当前版本不会保留字体、填充、边框、对齐、批注、合并单元格、条件格式、数据验证、图片、图表、Excel 表格、名称、宏等高级对象或样式。因此,它适合数据和关键格式迁移,不等同于完整复制工作簿。

工作原理

  1. 父进程通过 Excel COM 以只读方式打开源工作簿,并禁用宏及外部链接更新。
  2. 单元格数据和关键格式批量写入临时 SQLite 数据库。
  3. 父进程关闭 Excel,再启动独立 Python 子进程。
  4. 子进程使用 openpyxl 重建 .xlsx,成功后自动删除临时数据库。

批量转换是顺序执行,不会同时启动多个 Excel 实例。成功时返回退出码 0;失败时返回非零退出码并将错误信息写入标准错误流。

开发与构建

从源码安装:

git clone https://github.com/minionszyw/x2x.git
cd x2x
python -m pip install -e .

构建并检查发布包:

python -m pip install build twine
python -m build
python -m twine check dist\*

许可证

本项目采用 Apache License 2.0

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

x2x_excel-0.1.0.tar.gz (16.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

x2x_excel-0.1.0-py3-none-any.whl (14.8 kB view details)

Uploaded Python 3

File details

Details for the file x2x_excel-0.1.0.tar.gz.

File metadata

  • Download URL: x2x_excel-0.1.0.tar.gz
  • Upload date:
  • Size: 16.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.10

File hashes

Hashes for x2x_excel-0.1.0.tar.gz
Algorithm Hash digest
SHA256 d0c2a1feffccb6c50511f8032c93c79fefc6269efd55db0b8799c32cdabc229b
MD5 b6421d8a55d386c72d30b13d5d0ed034
BLAKE2b-256 bec15c51feba133cf8090aab8d3fa009cbfd4607958035fdb23d08d322dc6223

See more details on using hashes here.

File details

Details for the file x2x_excel-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: x2x_excel-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 14.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.10

File hashes

Hashes for x2x_excel-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a148e0965b053422aa1d812e86d7bf273754fa0cdaafbd4ff96c44776c4622d0
MD5 8f8f6c38cdca8fc8e8dd2c9e8382f517
BLAKE2b-256 c5f7857d20a81f823ef2b0b1c86488db3fb28d5c55269bebc3ec091ae557fd38

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page