10 examples of 'how to open an excel file in python' in Python

Every line of 'how to open an excel file 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
56def open_excel_input_file(self, path):
57 if not path or path == '-':
58 if six.PY2:
59 return six.BytesIO(sys.stdin.read())
60 else:
61 return six.BytesIO(sys.stdin.buffer.read())
62 else:
63 return open(path, 'rb')
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

90def open_content(self, file_content, skip_hidden_sheets=True, **keywords):
91 self._keywords = keywords
92 self._file_content = file_content
93 self.__skip_hidden_sheets = skip_hidden_sheets
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()
68def worksheet_from_excel(excel_sheet):
69 worksheet = Worksheet()
70 for col in range(excel_sheet.ncols):
71 for row in range(excel_sheet.nrows):
72 cell = excel_sheet.cell(row, col)
73 if cell.ctype == XL_CELL_ERROR:
74 formula = '=%s' % (error_text_from_code[cell.value], )
75 elif cell.ctype == XL_CELL_DATE:
76 formula = '=DateTime(%s, %s, %s, %s, %s, %s)' % xldate_as_tuple(
77 cell.value, excel_sheet.book.datemode)
78 else:
79 formula = unicode(excel_sheet.cell(row, col).value)
80 worksheet[col + 1, row + 1].formula = formula
81 return worksheet
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()
30def import_openpyxl(self):
31 """Import the requests module """
32 try:
33 import openpyxl
34 from openpyxl import load_workbook
35 except ImportError:
36 print("openpyxl module is not installed"\
37 "Please install openpyxl module to"\
38 "perform any activities related to parsing xl sheets")
39 else:
40 self.openpyxl = openpyxl
41 self.load = load_workbook
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)
11def processExcel(filePath, fileName):
12 if "." in fileName:
13 fileName = fileName.split('.')
14 fileName = fileName[0]
15 data = xlrd.open_workbook(filePath)
16 table = data.sheets()[0]
17 nrows = table.nrows
18 ncols = table.ncols
19
20 cs_fields_index = []#filed index
21 golang_fields_index = []#filed index
22 tableKeysIndex = []
23
24 if table.nrows == 0 or table.ncols == 0:
25 print("empty file:" + fileName)
26
27 for index in range(ncols):
28 CS_row = table.cell(1, index).value
29 if CS_row == "C" or CS_row == "CS":
30 cs_fields_index.append(index)
31
32 if CS_row == "S" or CS_row == "CS":
33 golang_fields_index.append(index)
34
35 if len(cs_fields_index) > 0:
36 cs_files.append(fileName)
37 GenCSTableManagerFile(fileName, cs_fields_index, table)
38 GenCSTableData(fileName, cs_fields_index, table)
39
40 if len(golang_fields_index) > 0:
41 go_files.append(fileName)
42 GenGoTableManagerFile(fileName, golang_fields_index, table)
43 GenGolangTableData(fileName, golang_fields_index, table)
29def load_workbook(self, filepath_or_buffer):
30 from xlrd import open_workbook
31
32 if hasattr(filepath_or_buffer, "read"):
33 data = filepath_or_buffer.read()
34 return open_workbook(file_contents=data)
35 else:
36 return open_workbook(filepath_or_buffer)
9def test_write_texttable():
10 content = [[1, 2]]
11 test_file = "test.texttable"
12 expected = dedent(
13 """
14 pyexcel_sheet1:
15 +---+---+
16 | 1 | 2 |
17 +---+---+"""
18 ).strip("\n")
19 pe.save_as(array=content, dest_file_name=test_file)
20 with open(test_file, "r") as f:
21 written = f.read()
22 eq_(written, expected)
23 os.unlink(test_file)

Related snippets