LoginSignup
1
2

More than 5 years have passed since last update.

UbuntuのPythonでCalcを操作する

Last updated at Posted at 2018-03-15

UbuntuのPython3で実行すると、エラーが出ることが多く、ネットでいろいろ調べていたら、もっと簡単でCalc以外のLibreOfficeも使用出来る方法が見つかりました。

UbuntuのPython3からLibreOfficeを操作する

実行した環境

Ubuntu Stdio 17.10
Python 3.6.3
LibreOffice 6.0

UbuntuのPythonでCalcを操作する

LibreOfficeバンドルのPythonにUbuntuのPython3のPYTHONPATHを通して、ある程度パッケージが使用出来るようになりましたが、全てのパッケージが使用出来るわけではありません。
使用出来ないパッケージがあるので、UbuntuのPython3からCalcを操作します。

下準備

1.python3-unoをインストールする。(updateも忘れずに)
$ sudo apt install python3-uno
2.PyOOCalcをダウンロードする。(Python3専用)
https://github.com/thepurple/pyoocalc

pyoocalc.pyをPYTHONPATHの通ったフォルダにコピーする。
  PYTHONPATHの調べ方。

   $ python3
   >>> import sys
   >>> sys.path
['', '/usr/lib/python36.zip', '/usr/lib/python3.6', '/usr/lib/python3.6/lib-dynload', '/home/ty/.local/lib/python3.6/site-packages', '/usr/local/lib/python3.6/dist-packages', '/usr/lib/python3/dist-packages', '/usr/lib/python3.6/dist-packages']

3.下記のグラフのサンプルプログラムを実行するには下記も必要です。

$ pip3 install matplotlib
$ sudo apt install python3-tk
LibreOfficeバンドルのPythonでサンプルプログラムを実行する。

pyoocalc-master/src/examples
にサンプルプログラムがあるので、LibreOfficeバンドルのPythonで実行します。
例.(自分の環境に合わせてパスを変更してください。)

$ /opt/libreoffice6.0/program/python example.py

LibreOfficeが起動し、example.odsを開いてシートの追加、削除、行のコピー等が実行されます。

sofficeを通信状態で起動した状態で実行する場合

UbuntuのPythonで折れ線グラフを作成します。

エラーで動作しない場合もあるようです。
Linux Mint 18.2
Python 3.5.2
LibreOffice 6.0.2.1
のPCではエラーがでて動作しません。現在は原因は不明。

UbuntuのPythonでサンプルプログラムを実行する時は、sofficeを通信状態で起動しておいた状態で、プログラムを実行しないと言語が英語のsofficeが起動しexample.odsを開く時にエラーが出ます。
pyoocalc.pyがsofficeを起動しているようなのですが、いろいろやってみましたが解決しませんでした。
通信状態でsofficeを起動する例.(ショートカットを作成すると便利)

$ /opt/libreoffice6.0/program/soffice --accept="socket,host=localhost,port=2002;urp;"

通信状態でsofficeを起動し、graph1.odsに下のイメージのようにデータを入力して保存した状態で、下記を実行します。

graph1.py
#!/usr/bin/python3
# -*- coding: utf-8 -*-

import os
import sys

#sys.path.append('./../')
import pyoocalc

import matplotlib.pyplot as plt
import numpy as np

#/opt/libreoffice6.0/program/soffice --accept="socket,host=localhost,port=2002;urp;"
#で先にsOfficeを起動しておく。 

doc = pyoocalc.Document()

if doc:
    print("PyOOCalc version:", doc.version)    # Get PyOOCalc version

    doc.open_document(os.getcwd() + "/graph1.ods")  #graph1.odsを開く

    sheet = doc.sheets.sheet("Sheet1") 
    B2=sheet.cell_value_by_index(1,1)
    C2=sheet.cell_value_by_index(2,1)
    D2=sheet.cell_value_by_index(3,1)
    E2=sheet.cell_value_by_index(4,1)
    F2=sheet.cell_value_by_index(5,1)
    G2=sheet.cell_value_by_index(6,1)
    H2=sheet.cell_value_by_index(7,1)

    B3=sheet.cell_value_by_index(1,2)
    C3=sheet.cell_value_by_index(2,2)
    D3=sheet.cell_value_by_index(3,2)
    E3=sheet.cell_value_by_index(4,2)
    F3=sheet.cell_value_by_index(5,2)
    G3=sheet.cell_value_by_index(6,2)
    H3=sheet.cell_value_by_index(7,2)

    #xdata = [1880, 1900, 1920, 1940, 1960, 1980, 2000]
    xdata = [B2, C2, D2, E2, F2, G2, H2]

    #          年: 横軸の値

    #ydata = [959596, 2014100, 3699428, 7354971, 9683802, 11618281, 12064101]
    ydata = [B3, C3, D3, E3, F3, G3, H3]

    #          人口: 縦軸の値

    plt.plot(xdata, ydata, color = '#007700', linestyle = 'solid', linewidth = 3)

    #     折れ線の描画:  'k-' 黒実線

    plt.title('Transition of Population (Tokyo)\n', fontsize = 15)

    #       グラフのタイトルとフォントサイズ

    plt.xlabel('Year')

    #     横軸のタイトル

    plt.ylabel('Population')

    #     縦軸のタイトル

    plt.show()

#    グラフの表示

graph1.png

グラフの部分のコードは下記のHPのサンプルをそのまま使用しました。

注)
Ubuntu StudioのPCでは動作するのですが、Linux Mint 18のPCでは
sheet = doc.sheets.sheet("Sheet1")
でエラーになります??調査中
uno.RuntimeException: Binary URP bridge disposed during call

PyOOCalc付属のサンプルプログラムも同様のエラーがでます。

折れ線グラフ
http://y-okamoto-psy1949.la.coocan.jp/Python/sampleprgs/DrawGraphs/LineChart/

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2