将文件合并到xlsx中,然后重新构建目录

时间:2015-09-15 06:54:24

标签: python excel file binary openpyxl

我有很多文件(' *。pl-pl')。我的脚本必须找到每个文件,并使用xlsx将它们合并到一个openpyxl文件中。

现在,我想重建这些文件,我想重建与原件相同的文件。

但写完后有问题:

(内容变量包含一个文件的内容(从一个excel单元格中读取))

with open(path,'w') as f:
    f.write(content.encode('utf-8'))

现在,我检查原始文件是否与新文件相同。这些文件中的文字似乎相同,但大小差异很小。当我使用WinDiff应用程序来检查它们时,会发现一些不同的操作,但它们表示它们是different in blanks only

您能否告诉我如何重建这些文件与以前一样? 或者这是正确的吗?

注意:我尝试重建它们以确保会有相同的编码等,因为合并的excel文件将用于翻译,然后翻译的文件必须重建而不是原件。

这是代码 - 它检查目录并将所有文件名和内容打印到一个临时文件中。然后,它创建一个excel文件 - 第一个。列是路径(能够重建目录),第二列包含文件的内容,其中新行已切换为' = '

def print_to_file():
    import os
    for root, dirs, files in os.walk("OriginalDir"):
        for file in files:
            text = []
            if file.endswith(".pl-pl"):
                abs_path = os.path.join(root, file)
                with open(abs_path) as f:
                    for line in f:
                        text.append(line.strip('\n'))
            mLib.printToFile('files.mdoc', abs_path + '::' + '*=*'.join(text)) #'*=*' represents '\n'


def write_it():
    from openpyxl import Workbook
    import xlsxwriter
    file = 'files.mdoc'
    workbook = Workbook()
    worksheet = workbook.worksheets[0]
    worksheet.title = "Translate"
    i = 0

    with open(file) as f:
        classes = set()
        for line in f:
            i += 1

            splitted = line.strip('\n').split('::')
            name = splitted[0]
            text = splitted[1].split('*=*')
            text = [x.encode('string-escape') for x in text]
            worksheet.cell('B{}'.format(i)).style.alignment.wrap_text = True
            worksheet.cell('B{}'.format(i)).value = splitted[1]
            worksheet.cell('A{}'.format(i)).value = splitted[0]
    workbook.save('wrap_text1.xlsx')
import openpyxl

def rebuild():
    wb = openpyxl.load_workbook('wrap_text1.xlsx')
    ws = wb.worksheets[0]
    row_count = ws.get_highest_row()
    for i in xrange(1, row_count + 1):
        dir_file = ws.cell('A{}'.format(i)).value
        content = ws.cell('B{}'.format(i)).value
        remake(dir_file, content)


import os
def remake(path, content):

    content = re.sub('\*=\*', '\n', content)
    result = ''
    splt = path.split('\\')
    file = splt[-1]
    for dir in splt[:-1]:
        result += dir + '/'
        # print result
        if not os.path.isdir(result):
            # print result
            os.mkdir(result)
    with open(path, 'w') as f:
        f.write(content.encode('utf-8'))


# print_to_file() # print to temp file - paths and contents separated by '::'
# write_it() # write it into the excel file
# rebuilt() # reconstruct directory

0 个答案:

没有答案