Skip to content

Instantly share code, notes, and snippets.

@hanxiaomax hanxiaomax/excel2po.py

Last active Jun 19, 2020
Embed
What would you like to do?
excel读写/po文件转excel
#coding=utf-8
#从excel中读取中文,然后写入到po文件中,需要手动复制开头的说明文件,否则poedit不能正常读取
import xlrd
workbook = xlrd.open_workbook('Cura_copy.xls')#打开一个excel
sheet = workbook.sheet_by_name(u'sheet1')#获取一个工作表
buf=sheet.col_values(1)
for i in range(0,len(buf)):
buf[i]=unicode(buf[i]).encode('utf-8')
result=open("result.po",'w')
i=0
with open("engCURA.po",'r') as f:
for line in f:
line=line.strip('\n')
if line=="msgstr \"\"":
# result.write("msgstr \""+buf[i]+"\""+"\n")
result.write("msgstr \""+buf[i]+"\""+"\n")
i+=1
else:
result.write(line+"\n")
result.close()
#coding=utf-8
import xlrd
workbook = xlrd.open_workbook('Cura_copy.xls')#打开一个excel
sheet = workbook.sheet_by_name(u'sheet1')#获取一个工作表
buf=sheet.col_values(1)
for i in range(0,len(buf)):
buf[i]=unicode(buf[i]).encode('utf-8')
result=open("result.po",'w')
i=0
with open("engCURA.po",'r') as f:
for line in f:
line=line.strip('\n')
if line=="msgstr \"\"":
# result.write("msgstr \""+buf[i]+"\""+"\n")
result.write("msgstr \""+buf[i]+"\""+"\n")
i+=1
else:
result.write(line+"\n")
result.close()
from sys import argv
import xlwt
script,infile,outfile=argv#return as strings
out_file=open(outfile,'w+')
is_multi=False
num=0
buf=""
with open(infile,'r') as in_file:
for line in in_file:
line=line.strip('\n')
if "msgstr" in line:
is_multi=False
out_file.write(buf.replace("\"","")+"\n")
buf=""
if ("msgid" in line) or is_multi :
num=num+int(not is_multi)
if line=="msgid \"\"":
is_multi=True
elif is_multi:
buf+=line
else:
# out_file.write(str(num)+"\t"+line+'\n')
out_file.write(line.replace("\"","").replace("msgid ",""))
out_file.close()
def write2exl(infile,outfile):
rfile = open(infile,'r')
rfile2= open("Cura_cn.po",'r')
buf = rfile.read().split('\n')
buf2 = rfile2.read().split('\n')
rfile.close()
workbook = xlwt.Workbook()
sheet = workbook.add_sheet('sheet1')
for i in range(len(buf)):
# print type(buf[i])
sheet.write(i,0,buf[i])
sheet.write(i,1,buf2[i].decode('utf-8'))
# sheet.write(1, 0, label = 'Row 0, Column 0 Value')
workbook.save(outfile)
write2exl(outfile,"Cura.xls")
from sys import argv
import xlwt
script,infile,outfile=argv#return as strings
out_file=open(outfile,'w+')
needbuf=False
num=0
buf=""
with open(infile,'r') as in_file:
for line in in_file:
line=line.strip('\n')
if "#" in line:
if buf!="":
out_file.write(buf.replace("\"","")+"\n")
elif needbuf:
out_file.write("\n")
needbuf=False
buf=""
if ("msgstr" in line) or needbuf :#and (line!="msgid \"\"")
num=num+int(not needbuf)
if line=="msgstr \"\"":
needbuf=True
elif needbuf:
buf+=line
else:
out_file.write(line.replace("\"","").replace("msgstr ","")+"\n")
out_file.close()

###第一版 包含文件

  • po2excel.py

分离po文件中的英文字符串msgid,读取由po2excel_cn.py生成的中文文本,并生成excel文件

  • po2excel_cn.py

分离po文件中的中文字符串mgsstr

  • excel2po.py 把excel文件中的翻译,插入到英文版po文件中

python po2excel_cn.py a.po a_cn.po 提取中文字符串 python po2excel.py a.po output.xls 提取英文字符串,读取中文字符串,生成excel

####TODO

  • 封装
  • 合并为一个文件
  • 同时处理中文英文字符串
  • 精简逻辑
  • 尝试使用正则
  • 自动恢复文件头部说明
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.