2017-11-19 5 views
0

나는 다음과 같은 코드가 있습니다openpyxl에서와 같이 gsheets의 행을 색칠하는 방법은 무엇입니까?

import pandas as pd 
import openpyxl 
from openpyxl.styles import PatternFill 
import matplotlib 
import matplotlib.cm as cm 
import numpy as np 
import pygsheets 
import os 

data = [ 
    [3, 'test', 1], 
    [1, 'test', 2], 
    [1, 'test', 3], 
    [2, 'test', 4], 
    [3, 'test', 5], 
    [3, 'test', 6], 
    [4, 'test', 7], 
    [9, 'test', 8]] 

write_path = "output2.xlsx" 
df = pd.DataFrame(data, columns=["value", "comment", "index"]) 

with pd.ExcelWriter(write_path) as writer: 
    df.to_excel(writer, sheet_name="Sheet1", index=False) 

wb = openpyxl.load_workbook(write_path) 
ws = wb.get_sheet_by_name("Sheet1") 

# Create a color map 
tohex = lambda r,g,b,a: '%02X%02X%02X%02X' % (a,r,g,b) 
tab20c = cm.tab20c(np.linspace(0, 1, 20)) 
tab20c = np.array(tab20c * 255, dtype=int) 
tab20c = iter([tohex(*tab20c[i,:]) for i in range(20)]) 

colours = {} 
next_colour = 'FFFF0000' # start with red 

for cells in ws.iter_rows(min_row=2, min_col=1, max_col=1): 
    cell = cells[0] 

    try: 
     colour = colours[cell.value] 
    except KeyError: 
     colours[cell.value] = next_colour 
     colour = next_colour 
     next_colour = next(tab20c) # get the next colour in the colormap 

    cell.fill = PatternFill(start_color=colour, end_color=colour, fill_type='solid') 

wb.save(write_path) 


#authorization 
gc = pygsheets.authorize(service_file='C:/Users/Mikadincic/Google Drive/Python/Jazler Database Project/Jazler Database Project-0e56629e2784.json', no_cache=True) 

#open the google spreadsheet (where 'PY to Gsheet Test' is the name of my sheet) 
sh = gc.open('Jazler spotovi') 

#select the first sheet 
wks = sh[0] 

#update the first sheet with df, starting at cell B2. 
wks.set_dataframe(dataframe_spotovi,(1,1)) 

내가 Google 시트에서이 작업을 수행 할 수있는 방법이 있나요를? xlsx에 글을 쓸 때 필 요한 파일을 다시 열지 말고 시트의 변경 사항을 실시간 미리보기로 보는 것이 훨씬 더 좋습니다.

Google 스크립트로 시도 (자동으로 xlsx를 스프레드 시트로 변환)했지만 xlsx 파일을 덮어 쓰면 작동하지 않습니다. 아무튼, 이것을 gspread 나 pygsheets에서 직접하는 것이 낫습니다. 의견이 있으십니까?

답변

0

코드에서 정확히 무엇을하고 있는지 잘 모르겠습니다. 그러나 당신이 세포를 색칠하고 싶다면, pygsheets (당신은 gspread에서 세포를 채색 할 수 없습니다)에서 세포를 색칠하기 위해 다음을 사용할 수 있습니다 :

c1 = wks.cell('A1') 
c1.color = (1.0,1.0,0.5,1.0) # Red, Green, Blue, Alpha 
관련 문제