27

Does anybody knows how to create a new xlsx file using openpyxl in python?

filepath = "/home/ubun/Desktop/stocksinfo/yyy.xlsx"
wb = openpyxl.load_workbook(filepath)
ws = wb.get_active_sheet()

What do I need to add?

Newboy11
  • 2,778
  • 5
  • 26
  • 40

3 Answers3

51

I'm sorry my head is turning around. :) This solves the problem

filepath = "/home/ubun/Desktop/stocksinfo/test101.xlsx"
wb = openpyxl.Workbook()

wb.save(filepath)

This will create a new file.

Documentation site contains quickstart on front page.

Robert Lujo
  • 15,383
  • 5
  • 56
  • 73
Newboy11
  • 2,778
  • 5
  • 26
  • 40
  • I'm doing this now, but openpyxl.Workbook() throws error. Simply wb = Workbook() works when already imported with from openpyxl import Workbook – Will Croxford Feb 07 '19 at 09:55
17

Install and Import openpyxl

import openpyxl

Create new workbook

wb = openpyxl.Workbook() 

Get SHEET name

Sheet_name = wb.sheetnames

Save created workbook at same path where .py file exist

wb.save(filename='Test.xlsx')
Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
arpit patel
  • 171
  • 1
  • 2
  • 1
    Please follow the guidelines on https://stackoverflow.com/help/how-to-answer since in the current form, your answer looks hastily written. – B--rian Aug 16 '19 at 07:37
4

This is covered in the documentation: https://openpyxl.readthedocs.io/en/stable/tutorial.html#saving-to-a-file

wb.save(…)

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
Charlie Clark
  • 18,477
  • 4
  • 49
  • 55