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

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

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

但是写完后有个问题:

(内容variables包含一个文件的内容(从一个Excel单元格中读取))

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

所以现在我检查一下,原始文件是否与新文件相同。 这些文件中的文本似乎是相同的,但是大小差别不大。 当我使用WinDiff应用程序来检查它们时,它发现了一些不同的东西,但是它们说它们只是different in blanks only

你能给我一个build议如何重build这些文件是一样的以前? 或者这是正确的?

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

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

 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