The sample of the process to be executed is the program that I wrote down so far. (1) Graph Excel data with matplotlib (1) (2) [Convert matplotlib graph to emf file format] (http://qiita.com/gitytm15/items/418ddcd8f58555d7433b) (3) I tried changing Inkscape of Raspberry Pi from 0.48 to 0.92
It's a Raspberry Pi, so I like that it doesn't bother me so much even if I start it all the time.
In order to run the program when you put xlsx and xls files in the shared [DATA] folder, first create a shared folder that can be accessed from anywhere.
[share] ← shared folder │ └ [DATA] Put xlsx, xls here so that the conversion program will be executed │ ├ [eps] eps file save └ [emf] Save emf file
There are many places to refer to in the search, but I referred to here for the sharing method by samba of Raspberry Pi.
[hiramine.com] (http://www.hiramine.com/physicalcomputing/raspberrypi3/samba_installconfigconnect.html)
Here, `/ home / pi``` is shared, but since it is a problem if the area below pi is completely visible, I decided to create a ``` [share]
folder under it. At first, I didn't match the folder names of ``
[share] and `` `path = / home / share
, and sharing didn't work, so I was worried for a while. The rough flow is as follows.
Installation
sudo apt-get update
sudo apt-get install samba
Rewriting the configuration file (added to the last line)
sudo nano /etc/samba/smb.conf
smb.conf
#Last added
[share]
path = /home/pi/share
read only = No
guest ok = Yes
force user = pi
sudo service smbd restart
You can now access it from Windows, Mac and Ubuntu.
It's concrete code on how to monitor and what it actually does, but there's already an easy-to-understand article. I used it as a reference. The one I used is `` `watchdog```
・ [Watchdog is quite convenient for file monitoring] (http://qiita.com/PyYoshi@github/items/9744a138c018734c69db) -[LaTex automatic compilation environment made with Python] (http://qiita.com/syutaro/items/414fc12eeb8960b3c29b) ・ [Watchdog 0.8.3 Example API Usage] (https://pypi.python.org/pypi/watchdog)
watchdog can be installed with pip install watchdog
. Just to be sure.
See sample1.py below for the code. This is the main part to monitor and execute.
easy explanation In addition to the import around the watchdog, import the module for execution.
import sample2
Here we do this to execute the code in sample2.py. (Replace this part with an arbitrary executable file)
sample2.py summarizes the two codes below. Graph Excel data with matplotlib (1) [Convert matplotlib graph to emf file format] (http://qiita.com/gitytm15/items/418ddcd8f58555d7433b)
Since the file update monitoring target is an Excel file here, set the extension like this.
#Files to be monitored
observed_file_type = ('.xls','.xlsx')
Set the shared folder of the Raspberry Pi created earlier to BASEDIR. Originally, BASEDIR indicates the location of the file body
#BASEDIR = os.path.abspath(os.path.dirname(__file__))
Although it is, it is commented out here. This time, the address of the shared folder of Raspberry Pi is specified as follows.
BASEDIR ='/home/pi/share/DATA'
Next is the execution part when an event occurs. This code will be entered for each event. I am throwing the path of the updated Excel file.
sample2.plot(event.src_path) #Execute when creating a file
When you execute sample1.py, Raspberry Pi will enter the monitoring mode of the specified folder. It will be executed each time you move the Excel file to the specified folder, edit it, and save it. This Excel file corresponds to the structure that puts X in A1 and Y in B1 in the XY axis plot.
sample1.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import time
import os
from watchdog.events import FileSystemEventHandler
from watchdog.observers import Observer
import sample2 #← Module to be executed according to the update(Here, sample2)
#Files to be monitored
observed_file_type = ('.xls','.xlsx')
#Setting
#BASEDIR = os.path.abspath(os.path.dirname(__file__))
#→ If you want to monitor the location of this program file
#Specify a shared folder for Raspberry Pi
#shared folder[share]In,[DATA]Placed the folder
BASEDIR ='/home/pi/share/DATA'
#Check if the modified file is monitored
def match(path):
return any([path.endswith(ft) for ft in observed_file_type])
#Event handler at the time of change
class ChangeHandler(FileSystemEventHandler):
def on_create(self, event):
if event.is_directory:
return
if match(event.src_path):
print('Create',event.src_path)
sample2.plot(event.src_path) #Execute when creating a file
def on_modified(self, event):
if event.is_directory:
return
if match(event.src_path):
print('Modified',event.src_path)
sample2.plot(event.src_path) #Execute when changing files
def on_deleted(self, event):
if event.is_directory:
return
if match(event.src_path):
print('delete',event.src_path)
#Do nothing when the file is deleted
if __name__ in '__main__':
event_handler = ChangeHandler()
observer = Observer()
observer.schedule(event_handler, BASEDIR, recursive=True)
print('start dir=',BASEDIR)
observer.start()
try:
while True:
time.sleep(0.1)
except KeyboardInterrupt:
observer.stop()
observer.join()
It is called when the Excel file is updated or a new file is created.
The Excel path will be thrown, so read this Excel file and graph it. After that, draw a graph and save it in eps format, and at the same time convert it from svg to emf file and save it. Delete the original svg file.
The required modules are: ・ Pandas ・ Matplotlib ・ Subprocess
sample2.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
import subprocess
import os
#Specify the location of the font file ubuntu(C:\Windows\Fonts\Any Font)
fp = FontProperties(fname="/usr/share/fonts/truetype/fonts-japanese-gothic.ttf")
def dataload(path): #Load into Pandas DataFrame
df=pd.read_excel(path)
return df
def graph(df):
#Read x-axis and Y-axis data from the data frame
#Specified by column
x=df[[0]]
y=df[[1]]
#Graph
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.set_yscale('linear')
#ax1.set_yscale('log')
#ax1.set_ylim(-1.1, 1.1)
#ax1.set_xlim(0,360)
ax1.set_title("Graph sample",fontdict = {"fontproperties": fp},fontsize=12)
ax1.set_xlabel("x axis",fontdict = {"fontproperties": fp},fontsize=12)
ax1.set_ylabel("y axis",fontdict = {"fontproperties": fp},fontsize=12)
ax1.scatter(x, y,s=1,c='r',label='sin')
ax1.legend(loc="upper right")
def graph_save(path):
SAVEDIR=os.path.dirname(path) #File Path
fname, ext = os.path.splitext( os.path.basename(path) ) #File name only
plt.savefig(SAVEDIR+'/eps/'+fname+".eps") #eps save
plt.savefig(SAVEDIR+fname+".svg") #Save as svg for emf conversion
call_cmd='inkscape ' + SAVEDIR+fname + '.svg -M ' + SAVEDIR+'/emf/'+fname +'.emf'
subprocess.call(call_cmd,shell=True)
os.remove(SAVEDIR+fname+".svg") #I don't need svg, so delete it
def plot(path):
#Called from a monitoring program
df=dataload(path) #Load Excel as a pandas dataframe
graph(df) #Graph drawing with matplotlib
graph_save(path) #Save and convert graphs
if __name__ == '__main__':
SAVEDIR='/home/pi/share/DATA/'
path='/home/pi/Desktop/sample.xlsx'
plot(path)
that's all