天天看點

【Python】Qt國際化ts檔案轉excel檔案(xml轉excel)

前言:

Qt國際化使用的ts檔案是xml格式的,如果主機上沒有安裝qt 的 linguist 語言家應用,則閱讀起來很不舒服。

注:本案例可用于xml轉excel

手段:

python    pandas    xml.dom

python有多個excel操作庫,比如:xlwings,win32com,... ,openpyxl 等等。網上有很多對比這些庫的文章 。這裡就不一一列舉

各個庫的優缺點了。

xml庫在python的預設安裝包裡就有,不需要額外安裝,使用也很簡單粗暴,使用教程:

​​https://www.runoob.com/python/python-xml.html​​

​​https://docs.python.org/3/library/xml.dom.html​​

庫選型:

github上pandas的社群參與熱度明顯高于xlwings,win32com資料不多,用起來也麻煩,還要求使用者具備一定的windows使用經驗。故選用的是 pandas 處理 excel , pandas 依賴 openpyxl,是以需要同時安裝這兩個庫。

注:win32com 可以了解為對windows com的封裝,理論上在windows環境下其功能是最強大的,但是用起來其實就等于在windows的api,與其這樣不如直接丢到c++工程裡做了。

可執行檔案:

為了操作友善,直接提供可用的 exe

使用說明:

目前已經将輸入檔案名稱寫死,故需要把要被轉換的檔案放到exe同級目錄下,同時修改檔案名為 en2zh.ts ,最終輸出為 en2zh.xlsx 。

效果圖:

en2zh.ts:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE TS>
<TS version="2.1" language="zh_CN" sourcelanguage="en">
<context>
    <name>AddrPanel</name>
    <message>
        <location filename="../qml/panels/xxx.qml" line="236"/>
        <location filename="../qml/panels/xxx.qml" line="906"/>
        <location filename="../qml/panels/xxx.qml" line="912"/>
        <location filename="../qml/panels/xxx.qml" line="921"/>
        <source>Enter Search</source>
        <translation>輸入搜尋</translation>
    </message>
    <message>
        <location filename="../qml/panels/xxx.qml" line="241"/>
        <source>No Record!</source>
        <translation>無記錄!</translation>
    </message>
    <message>
        <location filename="../qml/panels/xxx.qml" line="359"/>
        <source>"failed!</source>
        <translation>失敗!</translation>
    </message>
    <message>
        <location filename="../qml/panels/xxx.qml" line="1029"/>
        <source>Type</source>
        <translation>類型</translation>
    </message>
    <message>
        <location filename="../qml/panels/xxx.qml" line="1054"/>
        <source>All Types</source>
        <translation>所有類型</translation>
    </message>      

eh2zh.xlsx:

1 Enter Search 輸入搜尋
2 No Record! 無記錄!
3 "failed! 失敗!
5 Type 類型
6 All Types 所有類型

ps:

本案例可擴充為xml轉excel

需要源碼,需要定制化處理,或其他協助,可聯系 [email protected]