Skip to content

Instantly share code, notes, and snippets.

@SeongUgJung
Last active August 29, 2015 14:26
Show Gist options
  • Save SeongUgJung/eb1643beafc5c087649c to your computer and use it in GitHub Desktop.
Save SeongUgJung/eb1643beafc5c087649c to your computer and use it in GitHub Desktop.
Python xlsx 처리 방법
from openpyxl import Workbook, load_workbook
# https://docs.djangoproject.com/en/1.8/topics/http/file-uploads/
# https://docs.djangoproject.com/en/1.8/ref/files/uploads/
# or Custom File Upload Handler
# File Upload to Temporary or Memory
def xlrd(request):
# After Uploaded....
# if xlsx
wb = load_workbook(filename='/Users/jsuch2362/PycharmProjects/xlrd/empty_book.xlsx')
sheets = wb.get_sheet_names()
first_sheet = wb[sheets[0]]
return HttpResponse(first_sheet['D18'].value)
# xlsx Write and File Stream
from django.http import HttpResponse
from openpyxl import Workbook, load_workbook
from openpyxl.utils import get_column_letter
from openpyxl.writer.excel import save_virtual_workbook
def xlxs(request):
wb = Workbook()
ws1 = wb.active
ws1.title = "range names"
for row in range(1, 40):
ws1.append(range(600))
ws2 = wb.create_sheet(title="Pi")
ws2['F5'] = 3.14
ws3 = wb.create_sheet(title="Data")
for row in range(10, 20):
for col in range(27, 54):
_ = ws3.cell(column=col, row=row, value="%s" % get_column_letter(col))
return HttpResponse(save_virtual_workbook(wb), content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment