2017-10-04 9 views

答えて

1

のようなものだろうと想像これはxlsxwriterを使用しています。名前とIDを持つスプレッドシートを作成し、関連付けられた画像へのリンクを3つの別々の列に作成します。

答えはurllib.requestを使用して再現可能です(このモジュールは必要ありません.3つのテストイメージをダウンロードするだけです)。私はまた、ディレクトリを現在のディレクトリに設定しました。これはあなたが望むように変更することができます。また、私の答えでは、.pngファイルだけを探すように設定しましたが、他のファイル形式も探すように調整することができます。

import urllib.request 
import xlsxwriter 
import os 


#comment out the next 4 lines if you don't want to download 3 pictures 
url = 'https://upload.wikimedia.org/wikipedia/en/thumb/4/43/Ipswich_Town.svg/255px-Ipswich_Town.svg.png' 
urllib.request.urlretrieve(url, "pica_1.png") 
urllib.request.urlretrieve(url, "picb_2.png") 
urllib.request.urlretrieve(url, "picc_3.png") 


dir_wanted = os.getcwd() 
#uncomment the following line if you don't want the current directory 
#dir_wanted = "C:\\users\\doe_j" 


file_list = [file for file in os.listdir(dir_wanted) if file.endswith('.png')] 
full_path_list = [dir_wanted + '\\' + file for file in file_list] 

name_list = [] 
num_list = [] 

for item in file_list: 
    temp_list = item.rpartition('_') 
    name = str(temp_list[0]) 
    num = str(temp_list[2].rpartition('.')[0]) 
    name_list.append(name) 
    num_list.append(num) 


workbook = xlsxwriter.Workbook('pics_and_links.xlsx') 
ws = workbook.add_worksheet('Links') 

#adding column titles and making them bold 
bold = workbook.add_format({'bold': True}) 
ws.write('A1', "Name", bold) 
ws.write('B1', "Number", bold) 
ws.write('C1', "Link", bold) 

#putting the three lists we made into the workbook 
for i in range (0, len(full_path_list)): 
    row_num = i + 2 
    ws.write('A%d' % row_num, name_list[i]) 
    ws.write('B%d' % row_num, int(num_list[i])) 
    ws.write_url('C%d' % row_num, full_path_list[i]) 

#Set the width of the column with the links in it 
ws.set_column(2, 2, 40) 

workbook.close() 
+1

本当にありがとうございました! – Grantler

+0

素晴らしい。それが助けてくれてうれしい。 – patrickjlong1

1

私はopenpyxlまたはxlsxwriter経験がないが、私はopenpyxlのドキュメントに見れば、私はプログラムは、私はあなたが達成できる方法を示して答えを提供しています。この

from openpyxl import Workbook 
from openpyxl.styles import PatternFill 
from scipy.misc import imread 

wb = Workboo() 
ws = wb.active 

img = imread('image.jpg', mode='RGB') 
for i in range(len(img)): 
    for j in range(len(img[0])): 
     # TODO a method to set turn (3, 1) into 'D2' 
     index = excel_coordinate(i, j) 
     # TODO a method to change RGB in a hex value, perhaps imread also support hex, not sure 
     hexval = RGB2hex(img[i][j]) 
     cel = ws[index] 
     cel.PatternFill("Solid", fgColor=hexval) 
+0

後半に答えるために申し訳ありませんが、本当に助けをapreciate! – Grantler

1

あなたはpandasパッケージ使用してそれを行うことができます。

import glob 
import os 
import pandas as pd 

files_dir = '/home/username/files_dir' # here should be path to your directory with images 
files = glob.glob(os.path.join(files_dir, '*')) 
df = pd.DataFrame(columns=['name', 'id', 'hyperlink']) 

for i, full_filename in enumerate(files): 
    filename = os.path.basename(full_filename) 
    name, id_ = filename.split('_') 
    id_ = os.path.splitext(id_)[0] # remove file extension from id_ 
    hyperlink = '=HYPERLINK("file:///{}")'.format(full_filename) 
    df.loc[i] = [name, id_, hyperlink] 

df.to_excel('output_file.xlsx', index=False) 
関連する問題