写入xlsx文件成为一个痛苦

我正在尝试使用xlsxwriter库在Excel文件中写入一些数据,但是我无法完美地实现它。我尝试了很多来写入正确的单元格,但没有重复,但没有运气。

这里是我想要做的屏幕截图:

我想要的是这里

而我仍然得到的是:

我所取得的是这里

虽然我不知道如何防止这些重复,但我可以设置总收入和收入的价值,但是我不能在收入和费用下设置这些账户的价值。

以下是我想要添加的数据示例:

 [[{'account_type': u'sum', 'balance': -14112.2, 'company_id': 1, 'company_name': u'Company 1', 'in_type': '', 'level': 0, 'name': u'Profit and Loss', 'type': 'report'}, {'account_type': u'account_type', 'balance': 5887.78, 'company_id': 1, 'company_name': u'Company 1', 'in_type': '', 'level': 1, 'name': u'Income', 'type': 'report'}, {'account_type': u'other', 'balance': 5785.0, 'company_id': 1, 'company_name': u'Company 1', 'in_type': u'Income', 'level': 4, 'name': u'200000 Product Sales', 'type': 'account'}, {'account_type': u'other', 'balance': 102.78, 'company_id': 1, 'company_name': u'Company 1', 'in_type': u'Income', 'level': 4, 'name': u'201000 Foreign Exchange Gain', 'type': 'account'}, {'account_type': u'account_type', 'balance': -19999.98, 'company_id': 1, 'company_name': u'Company 1', 'in_type': '', 'level': 1, 'name': u'Expense', 'type': 'report'}, {'account_type': u'other', 'balance': -19999.98, 'company_id': 1, 'company_name': u'Company 1', 'in_type': u'Expenses', 'level': 4, 'name': u'211000 Foreign Exchange Loss', 'type': 'account'}, {'balance': 0, 'in_type': u'Income', 'level': 4, 'name': u'200 Product Sales'}], [{'account_type': u'sum', 'balance': 5749.99, 'company_id': 3, 'company_name': u'Company 2', 'in_type': '', 'level': 0, 'name': u'Profit and Loss', 'type': 'report'}, {'account_type': u'account_type', 'balance': 5749.99, 'company_id': 3, 'company_name': u'Company 2', 'in_type': '', 'level': 1, 'name': u'Income', 'type': 'report'}, {'account_type': u'other', 'balance': 5749.99, 'company_id': 3, 'company_name': u'Company 2', 'in_type': u'Income', 'level': 4, 'name': u'200 Product Sales', 'type': 'account'}, {'account_type': u'account_type', 'balance': -0.0, 'company_id': 3, 'company_name': u'Company 2', 'in_type': '', 'level': 1, 'name': u'Expense', 'type': 'report'}, {'balance': 0, 'in_type': u'Income', 'level': 4, 'name': u'200000 Product Sales'}, {'balance': 0, 'in_type': u'Income', 'level': 4, 'name': u'201000 Foreign Exchange Gain'}, {'balance': 0, 'in_type': u'Expenses', 'level': 4, 'name': u'211000 Foreign Exchange Loss'}], [{'account_type': u'sum', 'balance': -0.0, 'company_id': 4, 'company_name': u'Company 3', 'in_type': '', 'level': 0, 'name': u'Profit and Loss', 'type': 'report'}, {'account_type': u'account_type', 'balance': -0.0, 'company_id': 4, 'company_name': u'Company 3', 'in_type': '', 'level': 1, 'name': u'Income', 'type': 'report'}, {'account_type': u'account_type', 'balance': -0.0, 'company_id': 4, 'company_name': u'Company 3', 'in_type': '', 'level': 1, 'name': u'Expense', 'type': 'report'}, {'balance': 0, 'in_type': u'Income', 'level': 4, 'name': u'200 Product Sales'}, {'balance': 0, 'in_type': u'Income', 'level': 4, 'name': u'200000 Product Sales'}, {'balance': 0, 'in_type': u'Income', 'level': 4, 'name': u'201000 Foreign Exchange Gain'}, {'balance': 0, 'in_type': u'Expenses', 'level': 4, 'name': u'211000 Foreign Exchange Loss'}]] 

这是我试过的Python代码。我希望我能find一些帮助。

 list_or = [] col_space1_ = 8 row_space1 = 6 col_space2 = 8 col_space3 = 8 col_space4 = 8 col_space5 = 8 account_row = 12 for record in lines: for sub_record in record: if sub_record.get('in_type') == 'Income': list_or.append(1) number_of_acc = len(list_or)/len(lines) income_co = ((number_of_acc * 2) + 2) income_lines = income_co + account_row income_lines1 = income_co + account_row worksheet.write(row_space1 + 2, 0, "Total", data_cell_format) worksheet.write(account_row, 1, "Income", data_cell_format) worksheet.write(income_lines, 1, "Expenses", data_cell_format) for line in lines: for sub_line in line: if sub_line.get('account_type') == 'sum': worksheet.write(row_space1, col_space1_, sub_line['company_name'], data_cell_format) worksheet.write(row_space1 + 2, col_space1_, sub_line['balance'], data_cell_format) col_space1_ = col_space1_ + 3 if sub_line.get('name') == 'Income': worksheet.write(12, col_space2, sub_line['balance'], data_cell_format) col_space2 = col_space2 + 3 if sub_line.get('name') == 'Expense': worksheet.write(income_lines, col_space3, sub_line['balance'], data_cell_format) col_space3 = col_space3 + 3 if sub_line.get('in_type') == 'Income': worksheet.write(account_row, sub_line.get('level') - 2, sub_line['name'], data_cell_format) worksheet.write(account_row, col_space4, sub_line['balance'], data_cell_format) account_row = account_row + 2 col_space4 = col_space4 + 3 if sub_line.get('in_type') == 'Expenses': worksheet.write(income_lines1, sub_line.get('level') - 2, sub_line['name'], data_cell_format) worksheet.write(income_lines1, col_space5, sub_line['balance'], data_cell_format) income_lines1 = income_lines1 + 2 col_space5 = col_space5 + 3 

如果您遇到更复杂的问题,请简单一些! 这是一个经典的解决问题的技巧,而且在解决编程问题时也是非常有用的。 你的问题似乎与间距有关,所以让我们暂时做一个更简单的间隔:(PS,我想你忘记提及收入下的20万产品销售额,而错误地将211000外汇亏损列出了两倍):

 rows = ["Profit and Loss", "Income", "201000 Foreign Exchange Gain", "200000 Product Sales", # I think you forgot to mention this one "200 Product Sales", "Expense", "211000 Foreign Exchange Loss"] # Next, sort each sublist in the order we desire for sublist in lines: sublist.sort(key=lambda x: rows.index(str(x["name"]))) import numpy as np x = np.dstack((line for line in lines)) # Create file (Change this to your desired output path) f = open("C:\Users\Matthew\Desktop\stack_simple_spacing.xls", 'w') # Write out first row companies = [item[0]["company_name"] for item in lines] company_header_string = "".join("," + str(company) for company in companies) f.write(company_header_string + "\n") # Write out the rest of the rows (they're already sorted in the order desired) for row in x[0]: output = row[0]["name"] + "," for item in row: output += str(item["balance"]) + "," f.write(output + "\n") f.close() 

这是输出:

简单的间距

如果你真的想要间隔,你必须增加你的代码的复杂性,使其更难以阅读/理解。 但是修改我们现有的代码简单地包含间距(而不是试图在一个shebang中完成):

 rows = ["Profit and Loss", "Income", "201000 Foreign Exchange Gain", "200000 Product Sales", # I think you forgot to mention this one "200 Product Sales", "Expense", "211000 Foreign Exchange Loss"] indents = [0, 1, 2, 2, 2, 1, 2] # Next, sort each sublist in the order we desire for sublist in lines: sublist.sort(key=lambda x: rows.index(str(x["name"]))) # Stack data so each tuple has data for a row import numpy as np x = np.dstack((line for line in lines)) # Create file (Change this to your desired output path) f = open("C:\Users\Matthew\Desktop\stack_fancy_spacing.xls", 'w') # Write out first row companies = [item[0]["company_name"] for item in lines] company_header_string = (","*8) + "".join(str(company) + ",,," for company in companies) f.write(company_header_string + "\n") # Write out the rest of the rows for index, row in enumerate(x[0]): output = ","*indents[index] + row[0]["name"] + ","*(8 - indents[index]) for item in row: output += str(item["balance"]) + ",,," f.write(output + "\n") f.close() 

这是输出:

在这里输入图像说明

解释 Excel将解释csv(逗号分隔值)文件非常好。 每个逗号是连续项目之间的分隔符。 换行符"\n"表示新行的开始。 因此,当你看到",,," ,excel将其解释为一行中的两个空单元格(注意这个文字在计算company_header_string的逻辑中)。 您可以通过编辑代码中的8来使公司栏显示在一起(如果您希望这些栏靠近边缘,可将其设置为76 )。 这个任务不需要xlswritter库。