Created
October 12, 2017 00:59
-
-
Save dev001hajipro/9308527cb2495f2c05f04690710dca37 to your computer and use it in GitHub Desktop.
COMを使ってPythonからExcelを操作する簡単なソースコード
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
"""COMでPythonからExcelを操作する | |
""" | |
import os | |
from win32com.client import Dispatch | |
def main(): | |
print(os.getcwd()) | |
filename = os.getcwd() + '\hello_excel.xlsx' | |
# 存在する場合は削除 | |
if os.path.exists(filename): | |
os.remove(filename) | |
xl = Dispatch('Excel.Application') | |
print(xl) | |
wb = xl.Workbooks.Add() | |
ws = wb.Worksheets.Add() | |
cell = ws.Cells(1) | |
cell.Value = 'こんにちは、Python(UTF-8)で書き込み世界!' | |
wb.Close(True, filename) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment