7 examples of 'how to convert excel to csv in python' in Python

Every line of 'how to convert excel to csv in python' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
84def WriteToExcel():
85 f = open('wordbank.csv','w')
86 list = CountInstancesGlobal()
87 for twoplet in list:
88 f.write(str(twoplet[0]))
89 f.write(',')
90 f.write(str(twoplet[1]))
91 f.write('\n')
92 f.close()
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

13def test_xlsx(self):
14 with open('examples/test.xlsx', 'rb') as f:
15 output = xlsx.xlsx2csv(f)
16
17 with open('examples/testxlsx_converted.csv', 'r') as f:
18 self.assertEquals(f.read(), output)
16def copy_excel(excelpath1, excelpath2):
17 """复制excel,把 excelpath1 数据复制到 excelpath2"""
18 wb2 = openpyxl.Workbook()
19 wb2.save(excelpath2)
20 # 读取数据
21 wb1 = openpyxl.load_workbook(excelpath1)
22 wb2 = openpyxl.load_workbook(excelpath2)
23 sheets1 = wb1.sheetnames
24 sheets2 = wb2.sheetnames
25 sheet1 = wb1[sheets1[0]]
26 sheet2 = wb2[sheets2[0]]
27 max_row = sheet1.max_row # 最大行数
28 max_column = sheet1.max_column # 最大列数
29 for m in list(range(1, max_row + 1)):
30 for n in list(range(97, 97 + max_column)): # chr(97)='a'
31 n = chr(n) # ASCII字符
32 i = '%s%d' % (n, m) # 单元格编号
33 cell1 = sheet1[i].value # 获取data单元格数据
34 sheet2[i].value = cell1 # 赋值到test单元格
35 wb2.save(excelpath2) # 保存数据
36 wb1.close() # 关闭excel
37 wb2.close()
44def get_xls_to_list(excelname, sheetname):
45 """
46 读取excel表,返回一个list,只是返回第一列的值
47 return [1,2,3,4,5]
48 """
49 datapath = os.path.join(data_path, excelname)
50 excel = xlrd.open_workbook(datapath)
51 table = excel.sheet_by_name(sheetname)
52 result = [table.row_values(i)[0].strip() for i in range(1,table.nrows)]
53 return result
488def excel(self):
489 """Query data base and build nsf and doe excels."""
490 rc = self.rc
491 if not rc.people:
492 sys.exit("please rerun specifying --people PERSON")
493 if isinstance(rc.people, str):
494 rc.people = [rc.people]
495 since_date = get_since_date(rc)
496 target = rc.people[0]
497 query_results = self.query_ppl(target, since_date=since_date)
498 self.render_template1(**query_results)
499 self.render_template2(**query_results)
20def test_xls_with_sheet(self):
21 with open('examples/sheets.xls', 'rb') as f:
22 output = xls.xls2csv(f, sheet='data')
23
24 with open('examples/testxls_converted.csv', 'r') as f:
25 self.assertEquals(f.read(), output)
8def convert(csv_path):
9 with open(csv_path) as f:
10 data = Data(f)
11
12 table = Table()
13 table.set_header(["procedure", "time [s]", "speedup", ""])
14
15 reference_time = None
16 for name, time in data:
17 if reference_time is None:
18 reference_time = time
19
20 speedup = reference_time/time
21
22 time = '%0.5f' % time
23 bar = unicode_bar(speedup * 10)
24 speedup = '%0.2f' % speedup
25 table.add_row([name, time, speedup, bar])
26
27 def get_path():
28 basename = os.path.splitext(os.path.basename(csv_path))[0]
29 return basename + ".txt"
30
31 path = get_path()
32 with open(path, 'wt', encoding='utf-8') as f:
33 f.write(unicode(table))
34
35 print "%s created" % path

Related snippets