![](/img/trans.png)
[英]How to merge .csv files into one .xls file in multiple sheets using Python?
[英]Transforming an xls file containing multiple sheets into separate csv in python
我在下面附加了一個json文件。 我必須在python中讀取json文件。 該文件包含我的xls文件的路徑,該文件包含多張工作表,需要對其進行清理並將每張工作表輸出為單獨的csv文件。 關於我該怎么做的任何想法?
{ "file":{
"path":"C:/.../xyz.xlsx",
"sheetname":"Sheet1"
"Clean":{
"1":"A",
"2":"B",
"3":"C"
},
"Delete":{
"1":"D",
"2":"E"
},
"outfile":"C:/.../out_xyz.csv"
}
}
我提到了我下面附加的幾個鏈接,但我仍然徒勞!
從文件中讀取JSON?
如何將包含多個工作表的Excel(.xls)文件拆分為單獨的Excel文件?
將每個工作表保存在工作簿中以單獨的CSV文件
這個怎么樣?
使用Python和xlrd和xlwt。 參見http://www.python-excel.org
以下腳本應執行您想要的操作:
import xlrd, xlwt, sys
def raj_split(in_path, out_stem):
in_book = xlrd.open_workbook(in_path)
in_sheet = in_book.sheet_by_index(0)
first_row = in_sheet.row_values(0)
# find the rightmost 1 value in the first row
split_pos = max(
colx for colx, value in enumerate(first_row) if value == 1.0
) + 1
out_book = xlwt.Workbook()
out_sheet = out_book.add_sheet("Sheet1", cell_overwrite_ok=True)
# copy the common cells
for rowx in xrange(in_sheet.nrows):
row_vals = in_sheet.row_values(rowx, end_colx=split_pos)
for colx in xrange(split_pos):
out_sheet.write(rowx, colx, row_vals[colx])
out_num = 0
# for each output file ...
for out_col in range(split_pos, in_sheet.ncols):
out_num += 1
# ... overwrite the `split_pos` column
for rowx, value in enumerate(in_sheet.col_values(colx=out_col)):
out_sheet.write(rowx, split_pos, value)
# ... and save the file.
out_book.save("%s_%03d.xls" % (out_stem, out_num))
raj_split(*sys.argv[1:3])
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.