w-html2docx 1.0.28 → 1.0.29

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,24 +1,32 @@
1
-
2
- 0.可用pyinstaller或nuitka編譯,但nuitka編譯出檔案會小一點(4x mb),不過卻無法執行待研究
3
-
4
- 1.可先用一般編譯方式產生spec檔: pyinstaller -F htmlToDocx.py,就會產生htmlToDocx.spec
5
-
6
- 2.若有需要修改spec後,編譯指令改為對spec檔編譯: pyinstaller build.spec
7
-
8
- 3.使用build.bat進行編譯
9
-
10
- 8.編譯完複製htmlToDocx.exe至bin,使用bin\run.bat執行,傳入的b64可將物件
11
- {
12
- 'fpIn':'input.html',
13
- 'fpOut':'output.docx'
14
- }
15
- 序列化成字串再轉base64即可
16
-
17
- ----------------------------------------------------------------------
18
- 參考
19
-
20
- 解决方法:pyinstaller打包缺文件
21
- https://www.geek-share.com/detail/2773482967.html
22
-
23
- pyinstaller spec文件
24
- https://pyinstaller.readthedocs.io/en/stable/spec-files.html
1
+
2
+ 0.可用pyinstaller或nuitka編譯,但nuitka編譯出檔案會小一點(4x mb),不過卻無法執行待研究
3
+
4
+ 1.可先用一般編譯方式產生spec檔: pyinstaller -F htmlToDocx.py,就會產生htmlToDocx.spec
5
+
6
+ 2.若有需要修改spec後,編譯指令改為對spec檔編譯: python -m PyInstaller htmlToDocx.spec --noconfirm --clean (於srcPython內執行, 產物為dist/htmlToDocx.exe, 再複製至../src/htmlToDocx.exe)
7
+
8
+ 3.使用build.bat進行編譯
9
+
10
+ 4.建置環境須先安裝 pip install pyinstaller pywin32。2026-09-13 現行exe以Python 3.14.3 + PyInstaller 6.22.3 + pywin32 312建置
11
+
12
+ 8.編譯完複製htmlToDocx.exe至src,使用run.bat執行,傳入的b64可將物件
13
+ {
14
+ 'fpInSrc': 'input.html', #來源html絕對路徑
15
+ 'fpInTemp': 'tmp.docx', #docx模板絕對路徑
16
+ 'fpOut': 'output.docx', #輸出docx絕對路徑
17
+ 'fontFamilies': ['標楷體', 'Times New Roman'], #依序強制更改之字型
18
+ 'imgRatioWidthMax': 1, #圖片最大寬度比例(0至1)
19
+ 'imgRatioHeightMax': 1, #圖片最大高度比例(0至1)
20
+ }
21
+ 序列化成json字串再轉base64即可
22
+
23
+ 9.輸出約定: stdout與stderr皆為utf-8。成功時stdout印 success 且離開碼0;失敗時stdout印 error: 例外類別: 原因(如 error: com_error: (-2147221005, '無效的類別字串', None, None) 為Word未安裝) 且離開碼1。各調整步驟(字型、圖片縮放、行距、對齊、零寬空格)之例外只印至stderr不影響結果。任一步驟例外時仍會關閉已開啟之文件並結束Word
24
+
25
+ ----------------------------------------------------------------------
26
+ 參考
27
+
28
+ 解决方法:pyinstaller打包缺文件
29
+ https://www.geek-share.com/detail/2773482967.html
30
+
31
+ pyinstaller spec文件
32
+ https://pyinstaller.readthedocs.io/en/stable/spec-files.html
@@ -199,4 +199,55 @@ describe('WHtml2docx', function() {
199
199
  assert.strict.deepEqual(false, xml.includes(String.fromCharCode(0x200B)))
200
200
  })
201
201
 
202
+ it('指定非預設字型時確實套用, 且輸出資料夾不存在時自動建立', async function() {
203
+ this.timeout(60000)
204
+
205
+ //fdTmp, 測試專用暫存資料夾, 測前確認不存在, 測後刪除
206
+ let fdTmp = `./test/_tmp/WHtml2docx`
207
+ let fpOut2 = `${fdTmp}/sub/ztmp2.docx`
208
+ w.fsDeleteFolder(fdTmp)
209
+ assert.strict.deepEqual(false, w.fsIsFolder(fdTmp))
210
+
211
+ //fontFamilies2, 與預設值不同且來源html未使用之字型, 轉出文件出現該字型即證明opt.fontFamilies確實被送入轉檔器
212
+ let fontFamilies2 = ['新細明體', 'Georgia']
213
+ let r = await WHtml2docx(fpIn, fpOut2, { fontFamilies: fontFamilies2 })
214
+ assert.strict.deepEqual('ok', r)
215
+ let xml2 = getDocumentXml(fpOut2)
216
+ assert.strict.deepEqual(true, xml2.includes(`w:eastAsia="${fontFamilies2[0]}"`))
217
+ assert.strict.deepEqual(true, xml2.includes(`w:ascii="${fontFamilies2[1]}"`))
218
+
219
+ w.fsDeleteFolder(fdTmp)
220
+ })
221
+
222
+ })
223
+
224
+
225
+ describe('WHtml2docx input checks', function() {
226
+
227
+ //check
228
+ if (!isWindows()) {
229
+ return
230
+ }
231
+
232
+ //以下不需Word即可執行, 皆於轉檔前被擋下
233
+
234
+ it('fpInHtml不存在時reject並含路徑', async function() {
235
+ let err = null
236
+ await WHtml2docx('./test/no-such-file.html', './test/_tmp/WHtml2docx/x.docx')
237
+ .catch((e) => {
238
+ err = e
239
+ })
240
+ assert.strict.deepEqual(true, typeof err === 'string' && err.includes('fpInHtml[') && err.includes('does not exist'))
241
+ })
242
+
243
+ it('fpInTemp有給但不存在時reject並含路徑', async function() {
244
+ let err = null
245
+ await WHtml2docx('./test/ztmp.html', './test/_tmp/WHtml2docx/x.docx', { fpInTemp: './test/no-such-temp.docx' })
246
+ .catch((e) => {
247
+ err = e
248
+ })
249
+ assert.strict.deepEqual(true, typeof err === 'string' && err.includes('fpInTemp[') && err.includes('does not exist'))
250
+ assert.strict.deepEqual(false, w.fsIsFolder('./test/_tmp/WHtml2docx')) //於檢查階段即reject, 不會建立輸出資料夾
251
+ })
252
+
202
253
  })