- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试编写一个脚本来使用 Python 3.7 操作 Ubuntu Mint _LibreOffice Calc 表。出于某种原因,当我尝试导入 Uno 时出现错误。我可以从 Calc 内部运行一个宏调用这个
desktop = XSCRIPTCONTEXT.getDesktop()
model = desktop.getCurrentComponent()
active_sheet = model.CurrentController.ActiveSheet
write 'Hello World' in A1
active_sheet.getCellRangeByName("A1").String = "Hello World!
最佳答案
经过大量研究,我找到了如何使用 Python 3.7、PyCharm 和 MySQL 操作 Ubuntu LibreOffice Calc 表的方法。我是这一切的新手,所以请原谅我的编码,我仍在学习。我提供了我用来完成这个项目的信息源的链接。
导致我出现问题的主要障碍是不明白在通过套接字连接之前需要启动 LibreOffice。之后,我能够使用 Uno 和其他模块与 Calc 电子表格进行交互。我提供了所有 Py 脚本的副本。希望这能帮助像我这样刚接触这个环境的人。
# opens connection to mysql database GVWC
import mysql.connector
import pandas as pd
from gui import validateLogin
def conn_mysql():
validateLogin #opens input box for username and password
us, pw=validateLogin() #returns login info
cnx = mysql.connector.connect(user=us, password=pw ,host='localhost', database= 'GVWC') # connects to DB
pd.set_option('display.max_columns', None)
df = pd.read_sql("SELECT * FROM GVWC.2020", cnx) #runs query
return df, pd #returns data
print("Database connection resolved.")
# connection to libreoffice
import uno
import os
import time
def lo_conn():
#Open socket to LibraOffice with delay to allow program to wait for connection
os.popen('/usr/lib/libreoffice/program/soffice --accept="socket,host=localhost,port=2002;urp;StarOffice.ServiceManager"')
time.sleep(3) # Sleep for 3 seconds
#=======================================================================================================================================
# get the uno component context from the PyUNO runtime
localContext = uno.getComponentContext()
# create the UnoUrlResolver
resolver = localContext.ServiceManager.createInstanceWithContext(
"com.sun.star.bridge.UnoUrlResolver", localContext)
# connect to the running office
ctx = resolver.resolve("uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext")
smgr = ctx.ServiceManager
# create input box for login info
from tkinter import *
from functools import partial
def validateLogin(username, password):
#print("username entered :", username.get())
#print("password entered :", password.get())
global pw
pw = password.get()
global us
us = username.get()
return us, pw
#window
tkWindow = Tk()
tkWindow.geometry("500x100+500+300")
tkWindow.title('Tkinter Login Form - pythonexamples.org')
#username label and text entry box
usernameLabel = Label(tkWindow, text="User Name").grid(row=0, column=0)
username = StringVar()
usernameEntry = Entry(tkWindow, textvariable=username).grid(row=0, column=1)
#password label and password entry box
passwordLabel = Label(tkWindow,text="Password").grid(row=1, column=0)
password = StringVar()
passwordEntry = Entry(tkWindow, textvariable=password, show='*').grid(row=1, column=1)
validateLogin = partial(validateLogin, username, password)
#login button
loginButton = Button(tkWindow, text="Login", command=validateLogin).grid(row=4, column=0)
tkWindow.mainloop()
#main program to create spreedsheet and fill with data from mysql
import os
from workingConn3 import conn_mysql
from lo_conn_3 import lo_conn
def main():
conn_mysql # call mysql def connection
df,pd=conn_mysql() # pass table data
lo_conn #connect to Libreoffice
# open calc and fill spreedsheet
#=====================================================================================================================================
writer = pd.ExcelWriter('Test_DF.xlsx',
engine='xlsxwriter',
datetime_format='mm/dd/yyyy')
df.to_excel(writer, index=False, sheet_name='TEST',
startrow=5, header = 4)
#Get the xlsxwriter objects from the dataframe writer object.
workbook = writer.book
worksheet = writer.sheets['TEST']
#Add a header format.
header_format = workbook.add_format({
'font_color': 'white',
'text_wrap': True,
'font_name': 'Calibri',
'font_size': 11,
'fg_color': '#44546a'})
# Write the column headers with the defined format.
for col_num, value in enumerate(df.columns.values):
worksheet.write(5, col_num, value, header_format)
colwidths = {}
# Store the defaults.
for col in range(14):
colwidths[col] = 15
# Then set the column widths.
for col_num, width in colwidths.items():
worksheet.set_column(col_num, col_num, width)
#Center text of column
cell_format = workbook.add_format()
cell_format.set_align('center')
worksheet.set_column('E1:E100',15,cell_format)
worksheet.set_column('F1:F100',15,cell_format)
worksheet.set_column('M1:M100',15,cell_format)
# Freeze pane on the header row.
#
worksheet.freeze_panes(6, 0)
worksheet.autofilter('A6:O6')
#Button text
worksheet.write('A3', 'Press the button to say hello.')
# Add a button tied to a macro in the VBA project.
worksheet.insert_button('A1', {'macro': 'start',
'caption': 'Press Me',
'width': 80,
'height': 30})
# Close the Pandas Excel writer and output the Excel file.
writer.save()
writer.close()
#reopen excel file
os.popen('/usr/lib/libreoffice/program/soffice Test_DF.xlsx --accept="socket,host=localhost,port=2002;urp;StarOffice.ServiceManager"')
if __name__== "__main__" :
main()
https://medium.com/analytics-vidhya/macro-programming-in-openoffice-libreoffice-with-using-python-en-a37465e9bfa5
关于python-3.x - 使用 Python 使用 Uno 访问 LibreOffice Calc,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60663664/
Closed. This question does not meet Stack Overflow guidelines。它当前不接受答案。 想改善这个问题吗?更新问题,以使溢出。 4年前关闭。 o
我有一个包含多个列的表格。 我只想要 target 值与 alg 值不同的行。在这种情况下,target 是 1 并且 alg 是 0。因此,第 2、4 和 5 行应该保留,而第 3 行应该删除。如何
我有一个百分比表: A | B | C ------------------- 1| 12% | 22% | 42% ------------------- 2| 52% | 2%
我有一个 Libreoffice Calc 工作簿,其中有 60 多张纸,其中许多都有复制到它们的大量图像。我想用 Calc Basic 宏删除所有图像。我尝试了以下失败并出现基本运行时错误,提示 P
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 12 个月前关闭。 Improve
我有一个格式为 HH:MM 的时间值求和列。总和的格式为 [HH]:MM,可防止 HH > 23 时翻转。我希望将此值转换为十进制数表示形式。例如: A B 1 12
有没有办法将模板应用于现有文档? 它似乎只有在我创建一个全新的文档时才有效,而且我不想复制粘贴所有内容。 最佳答案 我正在寻找相同的解决方案。我发现的唯一一件事是一个 LibreOffice 扩展来做
LibreOffice Calc 中的什么公式将计算不同 Week_Number 出现的次数?答案应该是2。 最佳答案 这个问题之前已经被问过很多次了。一种方法是在 B6 中输入以下公式然后按 Ctr
假设我在 LibreOffice calc 中有以下电子表格: | A | B | C | D | ------------------- |1| 1 | 2 | | 2 | |2| 2 |
LibreOffice Calc 中是否有接受 的函数整数 x 和 整数 y 并以该位置吐出单元格的内容? 有一个功能ADDRESS吐出给定 [x;y] 的地址。问题是我不知道如何取消引用它。 最佳答
我希望能够在 LibreOffice Calc 中选择两个单元格,然后按某种魔术键,结果交换两个单元格的内容。 我怎么做? 我为 Excel 找到了这个解决方案,但它在 LibreOffice 中不起
我正在寻找一种使用 LibreOffice 的 Calc 从网站获取一些数据的方法。 我之前使用带有 IMPORTXML 函数的 Google 表格,但因为它非常不可靠,所以我想改用 Calc。 我的
我想从 LibreOffice Calc 中多次出现的字符中获取最后一次出现。 例如我有一个字符串 abc1ba2ac2adeaa43add .现在,如果我正在搜索 a它应该返回 18。 最佳答案 使
我想在 LibreOffice Calc(v5.4.4.2 但可以升级)中实现一些条件格式,以突出显示其值通过公式计算而不是直接输入的单元格。 例如,一个单元格包含 =A3 将突出显示,而 Hello
我想为 Libreoffice Writer 创建一个模板,其中包含应显示在所有页面上的信息的右列。 事实上,我想要一种右侧的页眉。 有什么办法可以解决我的问题吗? 最佳答案 您可以使用 anchor
我有一个 LibreOffice 3 电子表格(在 Ubuntu 11.04 上),其中有一列包含数百个超链接,这些超链接仅显示为“链接”。 我需要将这些转换为纯文本,或者更糟糕的是显示超链接而不是文
看起来您只能从“数据”->“排序”中选择 3 个条件。有没有办法(可能是程序化的)使用额外的标准? 最佳答案 不,不调整代码是不可能做到的。从理论上讲,调整它并消除此限制应该很容易。对于 3-5 版本
我需要提取“日期字符串”作为表示日期/时间数据的单独列。 | A | B | C -+-----------------
我在 Windows 计算机上安装了 LibreOffice。 LibreOffice 附带了捆绑的 python.exe(版本 3.3),允许您用 Python 编写 LibreOffice 宏。这
我在 .docx 文件中的方程是 当我将 word 文件保存到 .odt 时,这个等式变成了 如何避免这个反问号? 最佳答案 解决方案:编辑等式并在等号后键入双引号 ("")。 关于libreoffi
我是一名优秀的程序员,十分优秀!